1

I am trying to create a Date object from a string in UTC format. So I follow this example in this blog: https://www.robertpieta.com/local-utc-date-format-swift/

And I did

let isoDateString = "2020-05-31T04:32:27Z"
let date = ISO8601DateFormatter().date(from: isoDateString)

But when I run the code on Xcode 12, 'date' is nil. Is that something got change in Xcode 12 which made above example stop working?

Thank you.

hap497
  • 154,439
  • 43
  • 83
  • 99

1 Answers1

0

You can try this, using DateFormatter Class, I hope this helps you

let isoDateString = "2020-05-31T04:32:27Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
// This returns a date from the string date
let date = dateFormatter.date(from: isoDateString)
print("isoDateString >> \(isoDateString)")
print("date >> \(String(describing: date))")

The output is:

isoDateString >> 2020-05-31T04:32:27Z
date >> Optional(2020-05-31 09:32:27 +0000)