-6

I am parsing 2020-06-09T18:11:26.904Z and it returns this 09 Jun, 11:11 pm it should be 06:11 pm

I have tried this solution Parsing a ISO8601 String to Date in Swift but it also works like the same.

Here's my code.

let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let date = df.date(from: dateTime) else {
    return nil
}

df.dateFormat = "dd MMM, hh:mm a"
df.amSymbol = "am"
df.pmSymbol = "pm"
let stringFromDate = df.string(from: date)
return stringFromDate

I know I could ask it on the mentioned link but due to the new user I couldn't add comment on that.

  • 2
    Use: ISO8601DateFormatter() to get all the logic for free. – Pranav Kasetti Jun 09 '20 at 14:59
  • 2
    It looks like you have problem just with time zone. Try to set the correct time zone like this `df.timeZone = TimeZone(secondsFromGMT: 0)` – imike Jun 09 '20 at 15:01
  • where to set correct timezone? – Mir Muhammad Hadi Ali Jun 09 '20 at 15:02
  • Please check my updated comment above – imike Jun 09 '20 at 15:04
  • You also can initizalize time zone with its identifier, just start typing `TimeZone(` and autocomplete will show you all options – imike Jun 09 '20 at 15:05
  • @imike thanks it worked. – Mir Muhammad Hadi Ali Jun 09 '20 at 15:17
  • Hi Mir, searching for "ISO8601 Swift" yields 96 on this site alone. I've marked one of them as a duplicate. If you're still having trouble, you should ask about concretely what you're struggling with – Alexander Jun 09 '20 at 15:18
  • please unAcceept the answer ... i need to delete it ... – Jawad Ali Jun 10 '20 at 07:03
  • @Alexander-ReinstateMonica I tried that solution but that did not work for me that's why I ask a new question as I am new to the community I was not able to comment on that question. – Mir Muhammad Hadi Ali Jun 11 '20 at 10:16
  • @jawadAli You are free to go. – Mir Muhammad Hadi Ali Jun 11 '20 at 10:17
  • @MirMuhammadHadiAli You should mention that in a stackoverflow question. People appreciate seeing the background research/work you've done. You should ask concretely what didn't work when trying to follow the other solution – Alexander Jun 11 '20 at 17:37
  • @Alexander-ReinstateMonica Updated the question. Hope it is fine now! – Mir Muhammad Hadi Ali Jun 11 '20 at 22:57
  • @MirMuhammadHadiAli Much better! I think I know what's going on. For one, you should understand that `Date` represents an instant in time, with no timezone attached. Fundamentally, it's just a wrapper around a `Double`, which counts a number of seconds after the "reference date" (00:00:00 UTC on 1 January 2001). What's probably happening here is that you didn't specify the `timezone` of the formatter you're using to print the date, so it defaults to your system's timezone (as stated in [the documentation](https://developer.apple.com/documentation/foundation/dateformatter/1411406-timezone)) – Alexander Jun 12 '20 at 00:41
  • @Alexander-ReinstateMonica got your point thanks for explaining it well. I hope you can remove the rejection to my question now. – Mir Muhammad Hadi Ali Jun 14 '20 at 19:10
  • @MirMuhammadHadiAli Although I can't find a duplicate question for the "didn't set a timezone" mistake you made, so I'll answer here instead. – Alexander Jun 15 '20 at 13:00

1 Answers1

0

You didn't set the timezone of the output date, so it's defaulting to your systems' timezone rather than UTC (which is what you were expecting).

Alexander
  • 59,041
  • 12
  • 98
  • 151