Home > other >  What is the correct date format for this date-as-a-string? [duplicate]
What is the correct date format for this date-as-a-string? [duplicate]

Time:09-17

The back end is providing me with this date-as-a-string: 2021-09-10T12:57:01.671Z

I need to convert that string to a Date using the iOS DateFormatter; and to do so I need to set the formatter's dateFormat property.

I have tried all sorts of combinations, with no luck. Specifically I am struggling with the .671Z part.

What is the correct date format to use?

CodePudding user response:

You need "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" as date format

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
     
       
guard let date = dateFormatter.date(from: self) else {
 // fallback if date is not in correct format
    return nil
}
  • Related