【问题标题】:NSDateFormatter dateFromString returns nil in iOS 13 issue [duplicate]NSDateFormatter dateFromString 在 iOS 13 问题中返回 nil [重复]
【发布时间】:2020-01-31 16:00:38
【问题描述】:

NSDateFormatter dateFromString 在 iOS v13 更新之前返回了一个 NSDate 对象。但在 iOS v13 和 v13.1 中,它返回 nil NSDate 对象。如何解决这个问题?

从“设置 -> 常规 -> 日期和时间”更改日期和时间,然后取消勾选 24 小时制可解决此错误。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date = [formatter dateFromString:@"2019-10-02 09:10:15"];

预计不是零 *日期

iOS v12 和更早的版本没有这个问题,设置 Locale 不是强制设置日期格式。

【问题讨论】:

  • 如果取消勾选 24 小时可修复错误,请将您的小时单位替换为 hh
  • @Desdenova 使用“hh”在下午时间的输出中显示错误时间(例如:2019-10-01 14:36:56)
  • 您的代码在 iOS 13.1 上适用于我
  • 为您的日期格式化程序添加一个区域设置,例如[formatter setLocale:[NSLocale localeWithLocaleIdentifier:[NSLocale currentLocale].localeIdentifier]];
  • @Paulw11 当“设置”中的时间格式未设置为“24 小时制”时,它可以正常工作

标签: ios objective-c nsdateformatter


【解决方案1】:

我找到了解决此问题的方法。

[formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

@hyd00 以您的方式设置区域设置也可以解决此问题。非常感谢。

[formatter setLocale:[NSLocale localeWithLocaleIdentifier:[NSLocale currentLocale].localeIdentifier]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2013-10-18
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多