- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
由于某些原因,某些设备无法将字符串转换为日期。
这是我的日期转换代码:
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 设置有关,但我不知道是什么问题。
更多详情...
我已经跟踪到以下导致零返回的设置:
timezone - Asia/Muscat
local - en_GB
string - "2016-12-18 08:31:43"
但是当我在 Playground 上运行它时,我得到了一个有效日期:
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")
"Dec 18, 2016, 6:31 AM"
是什么导致 DateFromatter 返回 nil?
最佳答案
如果要向用户显示日期,不要设置固定日期格式字符串。如果您使用固定格式的日期,请先修复您的语言环境。
有关详细信息,请参阅 Technical Q&A QA1480标题为“NSDateFormatter 和 Internet 日期”。以下是相关摘录(格式化我的):
Q: I'm using NSDateFormatter to parse an Internet-style date, but this fails for some users in some regions. I've set a specific date format string; shouldn't that force NSDateFormatter to work independently of the user's region settings?
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. (…)
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
andNSDateFormatter.timeStyle
) or generate your date format string from a template (usingNSDateFormatter.setLocalizedDateFormatFromTemplate(String)
).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.
关于ios - 来自字符串的 DateFormatter 日期返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41206810/
我的日期时间格式字符串是:yyyy-MM-dd'T'HH:mm:ss.SSSZ 我正在使用 Joda Time 中的 DateTimeFormatter 以上述格式打印我的日期。 现在,将日期视为 2
这个问题已经有答案了: Getting date from [NSDate date] off by a few hours (3 个回答) 已关闭 5 年前。 我对 Date 进行了扩展,返回格式化
先决条件 在手机设置中设置时区 GMT+13(法莱、托克劳)。 实现 extension Date { func formatForDto() -> String { let
我经常在代码中使用 DateFormatter 并且没有发现任何问题,但后来我决定测试一个类进行大量迭代 - 我注意到一个巨大的内存泄漏。 泄漏的原因是 DateFormatter。 简单代码: va
这个问题已经有答案了: want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format (11 个回答) Change the format
我正在尝试将不带时区值(例如“31.05.2015”)的字符串转换为 NSDate。 我的 DateFormatter 有什么问题?它返回日期 2015-05-31 22:00:00 +0000当我尝
这个问题已经有答案了: NSDateFormatter show wrong year (1 个回答) 已关闭 3 年前。 我尝试格式化我的 UTC 日期,但结果似乎非常错误 日期格式化程序:
这个问题在这里已经有了答案: NSDate Format outputting wrong date (5 个答案) Getting date from [NSDate date] off by
我一直在使用以下代码来格式化日期值: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setLoca
这个问题在这里已经有了答案: Getting date from [NSDate date] off by a few hours (3 个答案) 关闭 5 年前。 我对返回格式化字符串的 Date
我从字符串中得出的日期总是 nil,我不知道为什么。 func getBirthdate() -> Date? { let formatter = DateFormatter()
我是 iOS 的新手,我想在我的约会中增加几毫秒,比如 (5)。我使用了下面的代码,但我没有得到我正在使用的确切日期 let str_TimeStamp = "2017-08-09 10:26:30.
我的 DateFormatter 有问题。 我的 iOS 应用程序与服务器通信并使用 If-Modified-Since header 和使用以下格式化程序创建的日期: modifiedSinceDa
首先要提前致谢。我已经使用 iOS/Swift 多年,这是我第一次看到这种奇怪的行为。这是代码: let dateFormatter = DateFormatter() dateFormatter.d
这个问题在这里已经有了答案: NSDateFormatter show wrong year (1 个回答) 关闭 6 年前。 我有以下代码: var components = DateCompon
在我的 Java 应用程序中,我使用 DateFormat 实例来解析日期输入。 DateFormat fmt; fmt = DateFormat.getDateInstance(DateFormat
代码: let isoDate = "1981-02-20T10:44:00+0800" let dateFormatter = DateFormatter() dateFormatter.timeZ
有谁知道 Java 中的 DateFormatter 不是那么挑剔?我的意思是我可以为它提供多种格式的日期,如果我提供它这样的格式: yyyy-MM-dd HH:mm:ss Z 用户可以输入: 201
我已经阅读了多篇关于此的文章,其中大部分是由于未设置特定时区而系统获取了当前时区。我的场景略有不同并给出了意想不到的结果,所以我会尝试详细解释。 我正在处理一个返回日期的后端,该日期分为日期和时间,存
我试图以我设备的语言显示格式化日期,但它一直以英语显示(即使设备设置为葡萄牙语)。 在我的 ViewController 中我创建了一个日期: let todaysDate: Date = Date(
我是一名优秀的程序员,十分优秀!