0

I am parsing some dates sent via JSON and until today its been working perfectly. I am using the code from this answer:

extension Formatter {
    static let iso8601: DateFormatter = {
        let formatter = DateFormatter()
        formatter.calendar = Calendar(identifier: .iso8601)
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        return formatter
    }()
}


extension String {
    var dateFromISO8601: Date? {
        let date = Formatter.iso8601.date(from: self)
        return date
    }
}

...
let date = crossingTime.dateFromISO8601!
...

Looking at the dates that are being sent and assigned to crossingTime the one that is causing date to be nil is "2018-08-27T16:00:09Z". However another that works perfectly fine is "2018-08-23T13:59:54.447Z"

Which looking at the dates its clear one date has the milliseconds and the one thats failed doesn't. Is it possible to support both date formats?

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
DevWithZachary
  • 3,545
  • 11
  • 49
  • 101

0 Answers0