0

I'm having trouble parsing a date string in the PKT time zone.

Here is some code to paste in a playground:

import Foundation

let dateText = "Tue, 27 Jun 2017 04:00 AM PKT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, d MMM yyyy hh:mm a zzz"
if let checkDate = dateFormatter.date(from: dateText) {
    let date = checkDate
    print(date)
} else {
    print("Could not parse")
}

If I change the time zone to "CDT" it works. However, I am trying to parse a date in Pakistan Standard Time (PKT) as that is the format I am parsing.

I found this handy tool and it says the format is invalid.

Using this table as a reference in formatting.

Documentation: https://developer.apple.com/documentation/foundation/nsdateformatter?language=objc

I see a references to similar questions How to get Current EST time IOS Swift

Christopher
  • 5,806
  • 7
  • 31
  • 41
  • Don't parse time zone abbreviations. Ever. In any language. They're not reliable. Lots of other questions and answers on this already. You need to know ahead of time that you want Pakistan, that's `"Asia/Karachi"`. I'm not a Swift developer, so I'll let someone else give the implementation. – Matt Johnson-Pint Jun 27 '17 at 01:22
  • Ok, thanks. Good to know this. I have latitude/longitude coordinates which could geo-locate to a time zone perhaps. However, it appears I may be able to simply extract the time itself as I need to compare it to a time in the identical time zone and don't necessarily need to parse it. Just realizing this now. – Christopher Jun 27 '17 at 15:21
  • For lat/lon => time zone, see [this q&a](https://stackoverflow.com/q/16086962/634824). There is a Swift library mentioned there. – Matt Johnson-Pint Jun 27 '17 at 16:46

0 Answers0