Date Formatter
A time formatter can format date / time string into a Swift/Obj-c Date object, vice-versa. It uses date format string to determine the format of the date / time string.
Date Format String
For detail of how to wrote the date format string, you can visit NSDateFormatter.com - Easy Skeezy Date Formatting for Swift and Objective-C, it is very helpful.
Below information comes from DateFormatter - Foundation | Apple Developer Documentation official document.
When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string. For most fixed formats, you should also set the locale property to a POSIX locale (
en_US_POSIX), and set the timeZone property to UTC.
1 | let RFC3339DateFormatter = DateFormatter() |
Trouble Shooting
2019-06-10: When you are sure that the date format is correct, but it is return nil after format the date on some devices, please setup a locale.
1 | let dateFormatter = DateFormatter() |
Without that, on some device, it will fail to format 24 hours format HH.
