[英]dateFromString returns nil for some values
I am getting nil
for some values while using dateFromString
in swift
. 我在
swift
使用dateFromString
时得到一些值的nil
。 I searched a lot but in vain. 我搜寻了很多,但徒劳无功。
Following is my code: 以下是我的代码:
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)
}
Output console: 输出控制台:
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
The format is same for all date strings still I get nil for few values. 所有日期字符串的格式都是相同的,但对于几个值我还是没有。 Why so?
为什么这样? Please help.
请帮忙。 Where am I getting wrong?
我哪里出问题了?
格式应该是24小时的HH ,即使您只获得12小时的值。
frmt1.dateFormat = "MMM, dd yyyy HH:mm a"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.