3

I have the need of parsing a date that looks like this:

"2018-03-08T17:37:09.2694781-00:00"

Notice the fractional seconds, I need to keep all 7 digits because then I need to send back to my server exactly the same date. So, I need to be able to convert that string to Date, do some stuff and then send it back to another server thus converting that Date back to String again and keeping the same format and fractional seconds, keeping the original date as String is not an option here.

I already tried this but it does not work because the original Date/Time gets modified for some unknown reason

    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.Axxx"

    let dateObj = formatter.date(from:("2018-03-08T17:37:09.2694781-00:00"))
    //This produces: "2018-03-08 00:44:54 UTC", nothing to do with the original date, it would be fine if converted to UTC, though

I also tried a formatter like

formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"

But that truncates my fractional seconds to 3 digits.

Daxito
  • 71
  • 8
  • This might help: [How to configure DateFormatter to capture microseconds](https://stackoverflow.com/questions/43123944/how-to-configure-dateformatter-to-capture-microseconds). – Martin R Mar 08 '18 at 19:57
  • 1
    "A" is "milliseconds in a day" so "SSSSSSS" is the correct one. The truncation to milliseconds is a "feature" of the date formatter. – Martin R Mar 08 '18 at 20:18
  • if you need to preserve your date as it is you should store your date as a Double (timeIntervalSinceReferenceDate) instead of a String. – Leo Dabus Mar 08 '18 at 20:58

0 Answers0