gpt4 book ai didi

ios - dateFromString 对于某些值返回 nil

转载 作者:行者123 更新时间:2023-11-30 12:48:00 26 4
gpt4 key购买 nike

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。为什么这样?请帮忙。我哪里出错了?

最佳答案

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

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

关于ios - dateFromString 对于某些值返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41375520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com