melvitax / DateHelper

A Swift Date extension helper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect ISO date with timezones for dates that are too old

jsgoupil opened this issue · comments

let d = Date(fromString:  "1500-01-01", format: .isoDate, timeZone: .utc)
d.toString(format: .isoDateTimeSec)

Output: 1499-12-31T16:07:02-075258

This is not a valid ISO date.

This gives me a valid ISO date

let d = Date(fromString:  "1500-01-01", format: .isoDate, timeZone: .utc)
let f2 = ISO8601DateFormatter()
f2.string(from: d)

Output: 1500-01-01T00:00:00Z

if you specify a timeZone when creating a date, you need to use a time zone when converting it to string

let date = Date(fromString:  "1500-01-01", format: .isoDate, timeZone: .utc)
print(date!.toString(format: .isoDateTime, timeZone: .utc))
// prints "1500-01-01T00:00+0000"

This is quite the hidden functionality. Since it works great with today's dates, but not old ones.

Adding it to documentation for next release