Santiago carmona gonzalez Santiago carmona gonzalez - 1 year ago 127
Swift Question

Converting server date with Z format to local nsdate

I need to convert this server date (Given from kinvey request) into local timezone.

I'm using the following code:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ss.sTZD"

The date format is this:

ect = "2016-08-28T16:30:06.553Z" or
lmt = "2016-08-28T16:30:06.553Z"

When I print the date it is nil, do you know what I'm doing wrong ?. I think it could be the end of the dateFormat

Answer Source

If your app can target only iOS7+, you can use format symbols described in:

Fixed Formats (in Data Formatting Guide)

Unicode Technical Standard #35 version tr35-31

second | S | 1..n | 3456 | Fractional Second - truncates (like other time fields) to the count of letters. (example shows display using pattern SSSS for seconds value 12.34567)

zone | X | 1 | -08,+0530,Z | The ISO8601 basic format with hours field and optional minutes field. The ISO8601 UTC indicator "Z" is used when local time offset is 0. (The same as x, plus "Z".)

So, to parse fractional second, use uppercase 'S',

and 'X' for timezone including "Z" as UTC.

Try this:

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"

(I escaped 'T' as it may be used as another time formatting symbol in the future.)

PS. Put a proper title for your issue. (Though I couldn't have found a thread describing the date format which interprets "Z" as UTC.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download