字符串中的DateFormatter日期返回nil [英] DateFormatter date from string returns nil

查看:197
本文介绍了字符串中的DateFormatter日期返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,某些设备无法将字符串转换为日期。

For some reason some devices fail to convert a string to a date.

这是我的日期转换代码:

This is my date converting code:

func dateFromString(string: String) -> Date? {
    let f = DateFormatter.init()
    f.dateFormat = "yyyy-MM-dd HH:mm:ss"
    return f.date(from: string)
}

现在,通过我的分析平台,我会在此返回nil时捕获。我不知道为什么会这样。我认为它与本地和时区或12 / 24h设置有关,但我无法弄清楚问题是什么。

Now, through my analytics platform I capture when this return nil. I have no clue why so. I assume it has something to do with local and maybe timezone or 12/24h settings but I can't figure out what is the problem.

更多细节。 ..

我跟踪了导致零回报的以下设置:

I have tracked the following settings that causes a nil return:


timezone - Asia / Muscat

timezone - Asia/Muscat

local - en_GB

local - en_GB

string - 2016-12- 18 08:31:43

string - "2016-12-18 08:31:43"

但是当我在游乐场运行时,我得到一个有效的日期:

But when I run this in playground I get a valid date:

let f = DateFormatter.init()
f.dateFormat = "yyyy-MM-dd HH:mm:ss"
f.locale = Locale.init(identifier: "en_GB")
f.timeZone = TimeZone.init(identifier: "Asia/Muscat")

let s = f.date(from: "2016-12-18 08:31:43")




2016年12月18日,上午6:31

"Dec 18, 2016, 6:31 AM"

什么原因导致DateFromatter返回nil?

推荐答案

如果你重新向用户显示你的约会,的不设置固定的日期格式字符串的。如果您使用的是固定格式的日期,请先修复您的区域设置

If you’re showing your date to the user, don’t set fixed date format string. If you are using fixed-format dates, fix your locale first.

有关详细信息,请参阅 Technical Q& A QA1480 ,标题为NSDateFormatter和Internet Dates。以下是相关的摘录(格式化我的):

For more details, see Technical Q&A QA1480 titled "NSDateFormatter and Internet Dates". Below are relevant excerpts (formatting mine):


问:我正在使用NSDateFormatter来解析互联网风格的日期,但是某些地区的某些用户无法使用此功能。我已经设置了一个特定的日期格式字符串;不应强迫NSDateFormatter独立于用户的区域设置工作吗?

答:不会。设置日期格式字符串似乎适用于大多数用户,这不是解决这个问题的正确方法。有许多地方格式字符串以意想不到的方式运行。 (...)

A: No. While setting a date format string will appear to work for most users, it's not the right solution to this problem. There are many places where format strings behave in unexpected ways. (…)

如果您使用的是用户可见日期,则应避免设置固定日期格式字符串,因为它很难预测您的格式字符串将如何在所有可能的用户配置中表达。相反,你应该限制自己设置日期和时间样式(通过 NSDateFormatter.dateStyle NSDateFormatter.timeStyle )或生成来自模板的日期格式字符串(使用 NSDateFormatter.setLocalizedDateFormatFromTemplate(String))。

If you're working with user-visible dates, you should avoid setting a fixed date format string because it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should limit yourself to setting date and time styles (via NSDateFormatter.dateStyle and NSDateFormatter.timeStyle) or generate your date format string from a template (using NSDateFormatter.setLocalizedDateFormatFromTemplate(String)).

另一方面, 如果您使用的是固定格式的日期,则应首先将日期格式化程序的区域设置设置为适合您的固定格式。在大多数情况下,要选择的最佳区域设置是en_US_POSIX,无论用户和系统偏好如何,都专门设计用于产生美国英语结果的语言环境。

On the other hand, if you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences.

这篇关于字符串中的DateFormatter日期返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆