dateFromString returns 某些值为 nil

dateFromString returns nil for some values

swift 中使用 dateFromString 时,某些值得到 nil。我搜索了很多但徒劳无功。

以下是我的代码:

let strDate = self.sortedDict.valueForKey("TIME").objectAtIndex(indexPath.row).objectAtIndex(0) as? String
        print(strDate);

        let st_date = frmt.dateFromString(strDate!)

        let frmt1:NSDateFormatter = NSDateFormatter()
        frmt1.locale = NSLocale(localeIdentifier: localeStr)
        frmt1.dateFormat = "MMM, dd yyyy hh:mm a"
        if st_date != nil {
            print(st_date)
}

输出控制台:

    Optional("September, 20 2015 10:00:00")
Optional(2015-09-20 10:00:00 +0000)

Optional("October, 04 2015 10:00:00")
Optional(2015-10-04 10:00:00 +0000)

Optional("October, 04 2015 14:00:00") // nil
Optional("October, 18 2015 15:00:00") // nil
Optional("September, 20 2015 14:00:00") // nil

Optional("September, 27 2015 10:00:00")
Optional(2015-09-27 10:00:00 +0000)
Optional("September, 27 2015 12:00:00")
Optional(2015-09-27 00:00:00 +0000)
Optional("September, 27 2015 14:00:00")

Optional("October, 03 2015 14:00:00") //nil
Optional("October, 03 2015 16:00:00") //nil

所有日期字符串的格式都相同,但对于少数值我仍然得到 nil。为什么这样?请帮忙。我哪里错了?

格式应为 HH 24 小时,即使您只获取 12 小时的值。

frmt1.dateFormat = "MMM, dd yyyy HH:mm a"