Search code examples
iosswiftiphonedateiso8601

Parse ISO8601 in swift


I am parsing 2020-06-09T18:11:26.904Z and it returns this 09 Jun, 11:11 pm it should be 06:11 pm

I have tried this solution Parsing a ISO8601 String to Date in Swift but it also works like the same.

Here's my code.

let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let date = df.date(from: dateTime) else {
    return nil
}

df.dateFormat = "dd MMM, hh:mm a"
df.amSymbol = "am"
df.pmSymbol = "pm"
let stringFromDate = df.string(from: date)
return stringFromDate

I know I could ask it on the mentioned link but due to the new user I couldn't add comment on that.


Solution

  • You didn't set the timezone of the output date, so it's defaulting to your systems' timezone rather than UTC (which is what you were expecting).