swift problem on calculate time difference - swift

I have develop a function that calculate the time difference between two date giving the two date as string here below the function
func calculateTimeDifference(startDate: String, endDate: String) -> Int {
print("START DATE 1: \(startDate)")
print("END DATE 1: \(endDate)")
let startDate = dateTimeFormatter.date(from: startDate)
let endDate = dateTimeFormatter.date(from: endDate)
print("START DATE 2: \(startDate)")
print("END DATE 2: \(endDate)")
guard let startDate = startDate,
let endDate = endDate else {
print("return 1")
return 0
}
let dateDifference = Calendar.current.dateComponents([.minute], from: startDate, to: endDate)
let minuteDifference = dateDifference.minute
guard let minuteDifference = minuteDifference else {
print("return 2")
return 0
}
//timeDifference = minuteDifference
print("TIME DIFFERENCE: \(minuteDifference)")
return minuteDifference
}
and the corresponding date formatter that I'm using
var dateFormatter : DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter
}
var timeFormatter : DateFormatter {
let formatter = DateFormatter()
formatter.timeStyle = .short
return formatter
}
var dateTimeFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "d, MMM y, HH:mm"
return formatter
}
also try too add before each return formatter: formatter.locale = Locale.current
so now the problem is if I try this code on the simulator it works perfectly this below is the output in console using the simulator:
START DATE 1: 13, mag 2022, 11:36
END DATE 1: 13, mag 2022, 12:06
START DATE 2: Optional(2022-05-13 09:36:00 +0000)
END DATE 2: Optional(2022-05-13 10:06:00 +0000)
when try on physical device the output is this:
START DATE 1: 13, mag 2022, 11:36
END DATE 1: 13, mag 2022, 12:06
START DATE 2: nil
END DATE 2: nil

I guess this related to different locale, as May is Maggio italian
var dateTimeFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "d, MMM y, HH:mm"
formatter.locale = .init(identifier: "it_CH") // for italian locale
return formatter
}
calculateTimeDifference(startDate: "13, mag 2022, 11:36", endDate: "13, mag 2022, 12:06")
and the results
START DATE 1: 13, mag 2022, 11:36
END DATE 1: 13, mag 2022, 12:06
START DATE 2: Optional(2022-05-13 09:36:00 +0000)
END DATE 2: Optional(2022-05-13 10:06:00 +0000)
TIME DIFFERENCE: 30

Related

Swift compare between time

I have the current time, I need to check if the current time is between two times.
But I'm having trouble, as you can see startDate and endDate print past dates.
Can you give me a hand?
func getDate() -> Bool {
let start = "07:00"
let end = "19:00"
let dateFormat = "HH:mm"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
let startDate = dateFormatter.date(from: start)
let endDate = dateFormatter.date(from: end)
let currentDate = Date()
guard let startDate = startDate, let endDate = endDate else {
fatalError("Date Format does not match ⚠️")
}
print(startDate < currentDate && currentDate < endDate)
print(startDate) //2000-01-01 06:00:00 +0000
print(endDate) //2000-01-01 22:59:00 +0000
print(currentDate) //2021-07-13 22:11:05 +0000
return startDate < currentDate && currentDate < endDate
}
You just need to set your DateFormatter defaultDate to the start of the current date. If you would like to allow it to work with midnight (24:00) time as well you just need to set the date formatter isLenient to true. Note that if you create your date formatter inside your method it will create a new date formatter every time you call this method:
extension Formatter {
static let time: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = .init(identifier: "en_US_POSIX")
formatter.dateFormat = "HH:mm"
formatter.defaultDate = Calendar.current.startOfDay(for: Date())
formatter.isLenient = true
return formatter
}()
}
func isTimeBetween(start: String, end: String) -> Bool {
Formatter.time.defaultDate = Calendar.current.startOfDay(for: Date())
guard
let start = Formatter.time.date(from: start),
let end = Formatter.time.date(from: end) else {
print("invalid time input")
return false
}
print(start.description(with: .current)) // Tuesday, July 13, 2021 at 11:00:00 PM
print(end.description(with: .current)) // Wednesday, July 14, 2021 at 12:00:00 AM
print(Date().description(with: .current)) // Tuesday, July 13, 2021 at 11:42:02 PM
return start...end ~= Date()
}
isTimeBetween(start: "23:00", end: "24:00") // true
This will print:
Tuesday, July 13, 2021 at 11:00:00 PM Brasilia Standard Time
Wednesday, July 14, 2021 at 12:00:00 AM Brasilia Standard Time
Tuesday, July 13, 2021 at 11:42:02 PM Brasilia Standard Time
You can use Calendar.current.date(bySetting...) to set the hour/second/minute of an existing date. Then, compare those results.
func getDate() -> Bool {
let currentDate = Date()
let startDate = Calendar.current.date(bySettingHour: 7, minute: 0, second: 0, of: currentDate)
let endDate = Calendar.current.date(bySettingHour: 19, minute: 0, second: 0, of: currentDate)
guard let startDate = startDate, let endDate = endDate else {
fatalError("Date creation failed ⚠️")
}
print(startDate < currentDate && currentDate < endDate)
print(startDate)
print(endDate)
print(currentDate)
return startDate < currentDate && currentDate < endDate
}

Using a NumberFormatter within a DateFormatter

I'm attempting to format my Date() to look like Saturday, June 12th • 5PM - 12PM. I've been able to solve the majority of this with the following DateFormatter():
var date_formatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "EEEE, MMMM d • HHa - HHa"
return formatter
}
Which results in Saturday, June 12 • 5PM - 12PM
The challenge I'm having is understanding how to add the ordinal suffix (i.e. 12 -> 12th). I've seen a bit on the NumberFormatter(), but am not entirely sure how to integrate the two.
EDIT: Ended up having to create two formats for the 5PM - 12PM logic.
This looks like:
var start_time_formatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "EEEE, MMMM d • HHa -"
return formatter
}
var end_time_formatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "HHa"
return formatter
}
with the following to display it in a view:
Text("\(self.create_event_vm.start_time, formatter: self.start_time_formatter) \(self.create_event_vm.end_time, formatter: self.end_time_formatter)")
I understand this is a bit funky and could use some refactoring, but I'm hoping to get the desired effect, test, then refactor.
First for the day suffix you can create below function
func getDaySuffix(from date: Date) -> String {
switch Calendar.current.component(.day, from: date) {
case 1, 21, 31: return "st"
case 2, 22: return "nd"
case 3, 23: return "rd"
default: return "th"
}
}
and combine with your codes :
let startDate = Date()
let endDate = Calendar.current.date(byAdding: .hour, value: 5, to: startDate)!
var startTimeFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "EEEE, MMMM d'\(getDaySuffix(from: startDate))' • ha - "
return formatter
}
var endTimeFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "ha"
return formatter
}
let startDateResult = startTimeFormatter.string(from: startDate) // "Thursday, June 18th • 2AM - "
let endDateResult = endTimeFormatter.string(from: endDate) // "7AM"
let finalResult = startDateResult + endDateResult // "Thursday, June 18th • 2AM - 7AM"
First a helpful extension Int+Ordinal.swift:
extension Int {
/// `Int` ordinal suffix
enum Ordinal: String {
/// Suffix for numbers ending in `1` except 11
case st = "st"
/// Suffix for numbers snding in `2` except 12
case nd = "nd"
/// Suffix for numbers ending in `3` except 13
case rd = "rd"
/// Suffix otherwise
case th = "th"
}
/// Get `Ordinal` from `Int` `self`
var ordinal: Ordinal {
var mod = self % 100
if mod == 11 || mod == 12 || mod == 13 {
return .th
} else {
mod = mod % 10
if mod == 1 {
return .st
} else if mod == 2 {
return .nd
} else if mod == 3 {
return .rd
} else {
return .th
}
}
}
}
Then using this extension and DateFormatter:
func string(from startDate: Date, to endDate: Date) -> String {
let formatter = DateFormatter()
// weekdayMonth
formatter.dateFormat = "EEEE, MMMM"
let weekdayMonth = formatter.string(from: startDate)
// day
formatter.dateFormat = "d"
var day = formatter.string(from: startDate)
if let dayValue = Int(day) {
day += dayValue.ordinal.rawValue
}
// timeFrom
formatter.dateFormat = "ha"
let timeFrom = formatter.string(from: startDate)
// timeTo
let timeTo = formatter.string(from: endDate)
return "\(weekdayMonth) \(day) • \(timeFrom) - \(timeTo)"
}
Running it:
let dateString = string(from: Date(), to: Date().addingTimeInterval(3600 * 4))
print(dateString) // "Wednesday, June 17th • 11PM - 3AM"
Though I guess in this example you don't want it running into the next day! :)
You are going through the wrong path. What you need is to create a DateInterval and use its DateIntervalFormatter to display it to the user. Note that 12PM it is already another day so your date interval representation is wrong:
let dateA = DateComponents(calendar: .current, year: 2020, month: 6, day: 12, hour: 17, minute: 0).date!
let dateB = DateComponents(calendar: .current, year: 2020, month: 6, day: 13, hour: 0, minute: 0).date!
let di = DateInterval(start: dateA, end: dateB)
let dif = DateIntervalFormatter()
dif.dateTemplate = "EEEEMMMMdh"
dif.string(from: di) // "Friday, June 12, 5 PM – Saturday, June 13, 12 AM"

Date format from url (JSON)

How would I be able to take the date format from a URL and turn It into 2 separate date values in SwiftUI. The format from JSON is 2019-11-06 18:30:00 and I'm trying to get it to show as Dec 5 and would also like it to separate the time and show 8:00PM, is this possible?
This is the code that references the start time:
let startTime: String
var startTime: String {
return self.post.startTime
}
Let's step around the fact that 2019-11-06 18:30:00 can't be represented as Dec 5 and 8:00PM and focus on the work flow you'd need.
The basic idea is to:
Convert the String to a Date, via a DateFormatter
Use a custom DateFormatter to format the Date to the required "date" value
Use a custom DateFormatter to format the Date to the required "time" value
This might look something like...
let startTime = "2019-11-06 18:30:00"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
if let date = formatter.date(from: startTime) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d"
let dateString = dateFormatter.string(from: date)
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "h:ma"
let timeString = timeFormatter.string(from: date)
} else {
print("Bad date/format")
}
In my testing, this outputs Nov 6 and 6:30PM
you can pass your string date to date with this function
func stringToDate(date: String, format: String) -> Date
{
let date2 = date.count == 0 ? getCurrentDate(format: "dd-MM-yyyy") : date
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.init(identifier: "América/Mexico_City")
dateFormatter.dateFormat = format
let dateDate = dateFormatter.date(from: date2)
return dateDate!
}
func getCurrentDate(format: String) -> String
{
let formD = DateFormatter()
formD.dateFormat = format
let str = formD.string(from:Date())
return str
}
let dateA = stringToDate(date: "2019-11-06 18:30:00", format: "yyyy-MM-dd HH:mm:ss")
if dateA < Date() //Date() always will be the current date, including the time
{
print("dateA is older")
}
else
{
print("dateA in newer")
}
play with the format examples formats

Converting string to date returning the day before

I have a date in a string with this format "2017-03-14" (yyyy-MM-dd) and i am trying to convert it into a string with this format "Tuesday, March 14, 2017".
This is my code:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2017-03-14")
Now if i print the value of "date", i get this:
2017-03-13 22:00:00 +0000
This is just the day before. Why is that ?
EDIT:
I need to compare date before formatting it.
var newDateString : String = ""
let date2 = Date()
let comp = date2.compare(date!)
if comp.rawValue == 0 {
newDateString = "TONIGHT"
} else {
dateFormatter.dateFormat = "EEEE, MMMM d, yyyy"
newDateString = dateFormatter.string(from: date!)
}
Thanks
The desired Format should be:
EEEE, MMMM dd, yyyy
All you have to do is to add after your code the following code snippet:
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
let string = dateFormatter.string(from: date!) // "Tuesday, March 14, 2017"
Remark:
I'd like to suggest to do optional binding for declaring the date, as follows:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
if let date = dateFormatter.date(from: "2017-03-14") {
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
let string = dateFormatter.string(from: date) // "Tuesday, March 14, 2017"
}
Your confusion is based on a misunderstanding of what Time and Date are. Evidently, you are currently located in a time zone that is 2 hours ahead of UTC (Coordinated Universal Time), previously known as Greenwich Mean Time (GMT).
When you ask the OS for a Date object converted from "2017-03-14" you get a date/time reference of Midnight the morning of 2017-03-14 in your time zone which is, correctly, 10:00 pm (22:00) then night before in UTC.
When you ask the OS for a Date object for now with Date() you get a date/time reference of now in your time zone, which will be two hours earlier in UTC.
To accurately evaluate your date string to say "is now earlier than 'tonight of 2017-03-14'" you will probably want to convert from "2017-03-14 23:59" (or 11:59 pm, or perhaps prior to the start of tonight's event of 8:00 pm, etc).
This will do your original comparison, but would work better as a function (although I'm not sure how you want to use it)...
var newDateString : String = ""
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
// set tonightDate to 1 minute before midnight, tonight, in local time
if let tonightDate = dateFormatter.date(from: "2017-03-14 23:59") {
// set nowDate to current local time
let nowDate = Date()
let comp = nowDate.compare(tonightDate)
if comp.rawValue <= 0 {
newDateString = "TONIGHT"
} else {
dateFormatter.dateFormat = "EEEE, MMMM d, yyyy"
newDateString = dateFormatter.string(from: tonightDate)
}
}
print(newDateString)
It is calculated time based on UTC so you are getting day before.You can get proper format using below code:
func chageDateFormat(date:String) -> String{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
guard let date = dateFormatter.dateFromString(date) else {
assert(false, "No date from string")
return ""
}
print(date)
dateFormatter.dateFormat = "EEEE, dd MMMM, yyyy"
let result = dateFormatter.stringFromDate(date)
return result
}
let resultFormateDate = chageDateFormat("2017-03-14")
For comparison you also need to convert Date() to proper format.Both date must be in same format.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = NSDate()
let strdate = dateFormatter.stringFromDate(date)
let resultFormateDate2 = chageDateFormat(strdate)
Now you can compare two strings
if resultFormateDate == resultFormateDate2{
print("True")
}

How do I find the beginning of the week from an NSDate?

I'm implementing a calendar view, and I'd like it to start at the beginning of the week containing a particular date. Eg. If the target date is Monday, Feb 29, 2016, and the current calendar is set to start on Sunday, I'd like my view to start with Sunday, February 28.
This seems like it should be straightforward:
let calendar = NSCalendar.currentCalendar()
let firstDate = calendar.nextDateAfterDate(targetDate,
matchingUnit: .Weekday,
value: calendar.firstWeekday,
options: .SearchBackwards)
But this fails with:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Exactly one option from the set {NSCalendarMatchPreviousTimePreservingSmallerUnits, NSCalendarMatchNextTimePreservingSmallerUnits, NSCalendarMatchNextTime} must be specified.'
I can get basically what I want with:
let firstDate = calendar.nextDateAfterDate(firstDate,
matchingUnit: .Weekday,
value: calendar.firstWeekday,
options: .MatchPreviousTimePreservingSmallerUnits)?
.dateByAddingTimeInterval(-7 * 84600)
But it seems like a bad practice, since sometimes the number of seconds in a day isn't 86400.
Is there a better way?
you can use Calendar method date(from: DateComponents) passing [.yearForWeekOfYear, .weekOfYear] components from any date it will return the first day of the week from the calendar used. So if you would like to get Sunday just use Gregorian calendar. If you would like to get the Monday as the first day of the week you can use Calendar .iso8601 as you can see in this answer
Xcode 12 • Swift 5.3 or later (works with previous Swift versions as well)
extension Calendar {
static let gregorian = Calendar(identifier: .gregorian)
}
extension Date {
func startOfWeek(using calendar: Calendar = .gregorian) -> Date {
calendar.dateComponents([.calendar, .yearForWeekOfYear, .weekOfYear], from: self).date!
}
}
usage:
Date().startOfWeek() // "Sep 20, 2020 at 12:00 AM"
If you would like to get the beginning of week at a particular timezone you just need to use a custom calendar:
var gregorianUTC = Calendar.gregorian
gregorianUTC.timeZone = TimeZone(identifier: "UTC")!
print(Date().startOfWeek(using: gregorianUTC)) // "2020-09-20 00:00:00 +0000\n"
Swift 4 Solution
I have figured out according to my requirement, where I have find out dates for following.
1. Today
2. Tomorrow
3. This Week
4. This Weekend
5. Next Week
6. Next Weekend
So, I have created Date Extension to get Dates of Current Week and Next Week.
CODE
extension Date {
func getWeekDates() -> (thisWeek:[Date],nextWeek:[Date]) {
var tuple: (thisWeek:[Date],nextWeek:[Date])
var arrThisWeek: [Date] = []
for i in 0..<7 {
arrThisWeek.append(Calendar.current.date(byAdding: .day, value: i, to: startOfWeek)!)
}
var arrNextWeek: [Date] = []
for i in 1...7 {
arrNextWeek.append(Calendar.current.date(byAdding: .day, value: i, to: arrThisWeek.last!)!)
}
tuple = (thisWeek: arrThisWeek,nextWeek: arrNextWeek)
return tuple
}
var tomorrow: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
var startOfWeek: Date {
let gregorian = Calendar(identifier: .gregorian)
let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
return gregorian.date(byAdding: .day, value: 1, to: sunday!)!
}
func toDate(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
}
USAGE:
let arrWeekDates = Date().getWeekDates() // Get dates of Current and Next week.
let dateFormat = "MMM dd" // Date format
let thisMon = arrWeekDates.thisWeek.first!.toDate(format: dateFormat)
let thisSat = arrWeekDates.thisWeek[arrWeekDates.thisWeek.count - 2].toDate(format: dateFormat)
let thisSun = arrWeekDates.thisWeek[arrWeekDates.thisWeek.count - 1].toDate(format: dateFormat)
let nextMon = arrWeekDates.nextWeek.first!.toDate(format: dateFormat)
let nextSat = arrWeekDates.nextWeek[arrWeekDates.nextWeek.count - 2].toDate(format: dateFormat)
let nextSun = arrWeekDates.nextWeek[arrWeekDates.nextWeek.count - 1].toDate(format: dateFormat)
print("Today: \(Date().toDate(format: dateFormat))") // Sep 26
print("Tomorrow: \(Date().tomorrow.toDate(format: dateFormat))") // Sep 27
print("This Week: \(thisMon) - \(thisSun)") // Sep 24 - Sep 30
print("This Weekend: \(thisSat) - \(thisSun)") // Sep 29 - Sep 30
print("Next Week: \(nextMon) - \(nextSun)") // Oct 01 - Oct 07
print("Next Weekend: \(nextSat) - \(nextSun)") // Oct 06 - Oct 07
You can modify Extension according to your need.
Thanks!
You can implement this as Date class extension or something. It should returns something like 2020-01-06 00:00:00 +0000
Xcode 11.3 Swift 5
func firstDayOfWeek() -> Date {
var c = Calendar(identifier: .iso8601)
c.timeZone = TimeZone(secondsFromGMT: 0)!
print(
c.date(from: c.dateComponents([.weekOfYear, .yearForWeekOfYear], from: Date()))!
)
}
The Calendar has a mechanism for finding date at the start of a given time interval (say week of year, or month) that contains a given date:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2017-01-07")
if let date = date {
let calendar = Calendar(identifier: .gregorian)
var startDate : Date = Date()
var interval : TimeInterval = 0
if calendar.dateInterval(of: .weekOfYear, start: &startDate, interval: &interval, for: date) {
print("Start of week is \(startDate)")
// prints "Start of week is 2017-01-01 06:00:00 +0000"
}
}
In order to get the user's locale settings respected correctly, you should use the user's Calendar firstWeekday property in the DateComponents. This is what I usually use:
// MARK: first day of week
extension Date {
/**
Finds the first day of the week the subject date falls into.
- Parameter calendar: The calendar to use. Defaults to the user's current calendar.
- Returns: The `Date` of the first day of the week into which the subject date falls.
`startOfWeek()` respects the user's locale settings, i.e. will automatically use Sunday/Monday/etc. as first
weekday based on the user's region and locale settings.
*/
func startOfWeek(using calendar: Calendar = .current) -> Date? {
var components = calendar.dateComponents([.weekday, .year, .month, .weekOfYear], from: self)
components.weekday = calendar.firstWeekday
return calendar.date(from: components)
}
}
Basically use
NSCalender
and
dateByAddingComponents
. For solving of you're problem try to use this code sample:
let cal = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.weekOfYear -= 1
if let date = cal.dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(0)) {
var beginningOfWeek: NSDate?
var weekDuration = NSTimeInterval()
if cal.rangeOfUnit(.CalendarUnitWeekOfYear, startDate: &beginningOfWeek, interval: &weekDuration, forDate: date) {
print(beginningOfWeek)
}
}
I had problems with all previous solutions, since they do not take into account user's calendar setting. Next code will be taking into account that.
extension Date {
var startOfWeek: Date? {
let calendar = Calendar.current
var components: DateComponents? = calendar.dateComponents([.weekday, .year, .month, .day], from: self)
var modifiedComponent = components
modifiedComponent?.day = (components?.day ?? 0) - ((components?.weekday ?? 0) - 1)
return calendar.date(from: modifiedComponent!)
}
var endOfWeek: Date? {
let calendar = Calendar.current
var components: DateComponents? = calendar.dateComponents([.weekday, .year, .month, .day], from: self)
var modifiedComponent = components
modifiedComponent?.day = (components?.day ?? 0) + (7 - (components?.weekday ?? 0))
modifiedComponent?.hour = 23
modifiedComponent?.minute = 59
modifiedComponent?.second = 59
return calendar.date(from: modifiedComponent!)
}
}