Open adam-fowler opened 1 month ago
I believe the issue here is that you’re using a literal Z
in your DateFormatter
’s dateFormat
. In ISO-8601, the Z indicates UTC. If you update your DateFormatter
to be "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
, both will give the same result:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 3600)!
let date = dateFormatter.date(from: "2021-06-21T21:10:15Z")
print(date!)
let iso8601DateFormatter = ISO8601DateFormatter()
iso8601DateFormatter.timeZone = TimeZone(secondsFromGMT: 3600)!
let date2 = iso8601DateFormatter.date(from: "2021-06-21T21:10:15Z")
print(date2!)
Output:
2021-06-21 21:10:15 +0000
2021-06-21 21:10:15 +0000
When parsing a date with
DateFormatter
is you set the timeZone of theDateFormatter
then the parsed date will be in that time zone. When parsing a date withISO8601DateFormatter
the timeZone parameter is ignored and the Date is always assumed to be UTC.The output from the following code is