I have the following string: 20180207T124600Z
How can I turn this into a Date object?
Here is my current code but it returns nil
:<
If you have a date such as:
let isoDate = "2018-12-26T13:48:05.000Z"
and you want to parse it into a Date, use:
let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
isoDateFormatter.formatOptions = [
.withFullDate,
.withFullTime,
.withDashSeparatorInDate,
.withFractionalSeconds]
if let realDate = isoDateFormatter.date(from: isoDate) {
print("Got it: \(realDate)")
}
The important thing is to provide all the options for each part of the data you have. In my case, the seconds are expressed as a fraction.