Swift: DateFormatter 无法解析日期
Swift: DateFormatter not able to parse date
我有一个特定的时间格式,我想用它来生成日期对象。
这是格式:
Fri, 17 Mar 2017 08:42:00 +0100
这是我的 Swift 代码,应该创建数据对象:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
let d = dateFormatter.date(from: value)
使用 dateFormatter.date 生成 Date 对象总是 return nil。但是我看不到我的格式字符串有什么问题。
谁能看到我的错误?
我已经测试过了,效果很好。
您能否测试此代码行以识别您的语言环境配置?
print(Locale.current.identifier)
我只对 DateFormatter 方法 returns
的可选结果做了一点改动
import Foundation
let the_date: String = "Fri, 17 Mar 2017 08:42:00 +0100"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
if let d = dateFormatter.date(from: the_date)
{
print(d)
}
我有一个特定的时间格式,我想用它来生成日期对象。 这是格式:
Fri, 17 Mar 2017 08:42:00 +0100
这是我的 Swift 代码,应该创建数据对象:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
let d = dateFormatter.date(from: value)
使用 dateFormatter.date 生成 Date 对象总是 return nil。但是我看不到我的格式字符串有什么问题。 谁能看到我的错误?
我已经测试过了,效果很好。
您能否测试此代码行以识别您的语言环境配置?
print(Locale.current.identifier)
我只对 DateFormatter 方法 returns
的可选结果做了一点改动import Foundation
let the_date: String = "Fri, 17 Mar 2017 08:42:00 +0100"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
if let d = dateFormatter.date(from: the_date)
{
print(d)
}