I am trying to convert Date object in string using DateFormatter but I am getting some weird behavior. I have a timestamp value (1513356296) which I want to convert in 24hour format along with date and timeZone (My current is -0800). Here is my code:
let date = Date(timeIntervalSince1970: Double(1513356296))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(secondsFromGMT: -(8*3600))
dateFormatter.locale = Locale.current
I am expecting a result value of 2017-12-15T08:44:56-0800 but I am getting 2017-12-15T8:44:56-0800. Everything is same except the hour value. The hour value is 8 instead of 08.
This is not happing with all the test, it happened in only one device and rest are fine.
I also tried by checking if hour value changes if I change device time setting to 12hour format from 24hour format to see what I get for HH and it was always giving me 08 in my case. Only one user was facing this issue.
What's wrong with my code? or is there something wrong with HH I am using?
Related
This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 1 year ago.
I am trying to get date format of a date that I am getting from server.
What will be the format of this date:
2021-10-14T17:53:03.753588+05:30
What I tried:
yyyy-MM-dd'T'HH:mm:ssXXX
But Its not working.
How to (my tips & steps):
When you are struggling to find the date format for String -> Date ask you this: What my format is really doing? What's it's parsing/interpreting? Just let's see with Date -> String...
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
print(formatter.string(from: Date()))
Output: 2021-10-14T13:06:38+02:00: Is it the "same" as the string we have? No, some are the same, but not all...
Let's continue with another tip:
Let's put our format and the string one above the other one:
2021-10-14T17:53:03.753588+05:30
yyyy-MM-dd'T'HH:mm:ssXXX
Then, let's add "spaces", to make each pattern match its corresponding input:
2021-10-14 T 17:53:03.753588 +05:30
yyyy-MM-dd 'T' HH:mm:ss XXX
Then, let's check the doc (it's bookmarked in my web browser) for interpretation of the pattern and check if they correspond if needed, and to find the missing one if needed too.
Ok, so we aren't interpreting .753588 at all, that's why it's failing...
It's for the fractional seconds, so if we change the format to: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", it should work. Note, you can replace XXX with Z if you want...
Now, remember that patterns are case sensitive, so if you have strange hours, minutes, or nil because of that, check if you didn't misuse minutes vs month, 12h format vs 24 hour format...
If you have hours diff (or usually 30min diff), the issue could be then a timezone issue.
If you have a day diff, it could also be a timezone issue (interpret it as hours diffs around midnight, so there is a day change).
If you have a year diff, check if you didn't misuse yyyy vs YYYY.
Etc. But that should cover most of your cases (basic issues).
Try this format.
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"
As the date format you mentioned in your question contains milliseconds as well as timezone offset info.
Once date is parsed then based on your need you can set output date format and will get formatted date.
let sampleDate = "2021-10-14T17:53:03.753588+05:30"
let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX" // Set input date format.
let inputDate = dateFormatter.date(from: sampleDate)
print("Input Date:- \(inputDate)")
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" // Set output date format as per need
let outputDate = dateFormatter.string(from: inputDate ?? Date())
print("Output Date:- \(outputDate)")
With some digging, and help from a thread on SO, I was able to figure out that if you set up a DateFormatter with code like this:
let locale = NSLocale.current
let format : String = DateFormatter.dateFormat(fromTemplate: "j:mm.ss.SSS", options:0, locale:locale)!
print("format = \(format)")
let formatter = DateFormatter()
formatter.dateFormat = format
You get a date formatter with a date string that adapts to the user's 12/24 hour clock setting.
If you run that code in the US with the device set to 12 hour time, you see the following on the console:
format = h:mm:ss.SSS a
If you set your device to 24 hour time, you instead see
format = HH:mm:ss.SSS
That makes sense.
However, once you assign the resulting format string to your date formatter, it should be fixed. I would expect the date formatter to generate 12 or 24 hour times until I explicitly change it's format string.
That is not what happens. If I switch to settings and change the clock settings from 12 to 24 hours, the date formatter magically starts producing 24 hour time strings. I can log the date formatter's format string, and it sill shows the 12 hour string "h:mm:ss.SSS a". What's going on?
I have a separate DatePicker and TimePicker component in my app.
Once the user has selected both the desired Date and Time, I construct a new Date object like this:
let timeStamp = Date(year: selectedDate.year, month: selectedDate.month, day: selectedDate.day, hour: selectedTime.hour, minute: selectedTime.minute)
I then use DateFormatter to output the exact time that the user has selected like this:
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
formatter.string(from: timeStamp)
Now I have a very weird bug where sometimes time output will be correct (time will be displayed in UTC+2) and sometimes it'll be incorrect (time will be displayed in UTC+1) and I have absolutely no idea what could be causing this.
Example 1 (correct output):
User selects: May 26, 2020 - 18:38
Date ISO output: "2020-05-26T16:38:00Z"
DateFormatter output: "18:38"
This is the correct output
Example 2 (wrong output):
User selects: March 26, 2020 - 18:38
Date ISO output: "2020-03-26T16:38:00Z"
DateFormatter output: "17:38"
This is not the correct output. Time should be 18:38 like in the above example.
Someone please tell me how is this possible? Literally the only difference is user picked March instead of May (different month) and that for some reason confuses the DateFormatter, so Time output is in a different timezone.
I am using SwiftDate to work with dates in general.
Set correct formatter.locale, you can try Locale(identifier: "en_US_POSIX") or try to use formatter.timeZone property. Maybe TimeZone.current or TimeZone(secondsFromGMT: 0) will fix your problem
That is probably because in May daylight saving is in effect and the difference to UTC changes from +1 to +2 hours
You can use the current TimeZone and add configure your DateFormatter with it
let timezone = TimeZone.current
dateFormatter.timeZone = timezone
That should make sure that you always use the same timezone that is currently used by your device
The YLMoment library doesn't display the correct pretty date in relation to the user's local date/time settings. It shows 'an hour ago' for nearly everything, even though I only added the object on the server a second ago.
This was a basic error on my part.
When retrieving an object from your server / API, parse the date using the NSDateFormatter, but ensure it's time zone is set to UTC (assuming that's what it has been set to on your server).
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.dateFromString(YOUR_SERVER_DATE_STRING)
Hope this helps someone, because it had me confused for a while.
I am trying to compare the date chosen on a calendar (Kal calendar implementation) with the current date. The issue is that the current date is in MM/dd/yyyy format, whereas the Kal dates will have no leading zeroes if the month or day is below 10. Is there an easy way to retrieve the current date so that it will not have leading zeroes for the day or month if it's under 10? (The current date will actually be utilized as an attribute for saved objects, so that they can later be queried using the date selected with Kal)
example current date - 07/07/2014
example Kal date - 7/7/2014
Don't compare strings holding dates. Create actual NSDate objects, using an NSDateFormatter, and compare those. The format strings you need are "MM/dd/yyyy" for your date, however you're retrieving it, and "M/d/yyyy" for Kal's date.
I have used en_US_POSIX along with "MM/dd/yyyy" & it's Working Fine
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MM/dd/yyyy"
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW7