Convert ISO 8601 to NSDate

前端 未结 4 428
星月不相逢
星月不相逢 2020-12-03 01:02

I have a timestamp coming from server that looks like this:

2013-04-18T08:49:58.157+0000

I\'ve tried removing the colons, I\'ve tried all

4条回答
  •  心在旅途
    2020-12-03 01:55

    The perfect and best solution that worked for me is:

    let isoFormatter = ISO8601DateFormatter();
    isoFormatter.formatOptions = [ISO8601DateFormatter.Options.withColonSeparatorInTime,
                                          ISO8601DateFormatter.Options.withFractionalSeconds,
                                          ISO8601DateFormatter.Options.withFullDate,
                                          ISO8601DateFormatter.Options.withFullTime,
                                          ISO8601DateFormatter.Options.withTimeZone]
    let date = isoFormatter.date(from: dateStr);
    

    For further more detail, you can refer to apple's official documentation: https://developer.apple.com/documentation/foundation/nsiso8601dateformatter

提交回复
热议问题