-2

I'm trying to get the Date object for this string format "2018-03-30T14:36:10.093" in Swift, but I don't get it. It is not ISO8601 because of that point after the number 10.

Any solutions?

abnerabbey
  • 125
  • 1
  • 10

1 Answers1

-3

I got it. It's about to trim the string from the point after the 10 number and give the date formatter the following format:

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
abnerabbey
  • 125
  • 1
  • 10
  • If you trim your string you will be discarding the date fractional seconds. – Leo Dabus Jan 09 '20 at 18:26
  • 1
    No, now you’ve thrown away part of the number, resulting in a different date. – matt Jan 09 '20 at 18:26
  • `let s = "2018-03-30T14:36:10.093"; let f = ISO8601DateFormatter(); f.formatOptions = .withFractionalSeconds; let d = f.date(from: s)` – matt Jan 09 '20 at 18:38
  • @matt this is wrong. First the string should end with the timezone `Z`. Second you need to pass a set `[.withInternetDateTime, .withFractionalSeconds]` or simply `f.formatOptions.insert(.withFractionalSeconds)` – Leo Dabus Jan 09 '20 at 19:23
  • 1
    @LeoDabus Oh well. The point is your answer has this covered and I've pointed to it appropriately. I was just trying to show that the OP's string is not vitiated by the decimal fraction as the OP claimed. – matt Jan 09 '20 at 19:31