NSDateFormatter:IST not recognised

2019-09-17 11:01发布

问题:

IST timezone is not recognised by NSDateFormatter. Following date is recognised

let dateString = "Jun 10, 2015 @ 12.30 AM PDT"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM-dd-yyyy @ hh-mm-a-zzz"
let date = dateFormatter.dateFromString(dateString)

but time with IST timezone is not recognised.

let dateString = "Jun 10, 2015 @ 12.30 AM IST"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM-dd-yyyy @ hh-mm-a-zzz"
let date = dateFormatter.dateFromString(dateString)

Date is nil for IST timezone.

回答1:

As a workaround this helps:

let dateString = "Jun 10, 2015 @ 12.30 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM-dd-yyyy @ hh-mm-a"
dateFormatter.timeZone = NSTimeZone(abbreviation: "IST")
let date = dateFormatter.dateFromString(dateString)