[英]Swift ISO-8601 date formatting with iOS7
I really need your help guys.我真的需要你们的帮助。 The following piece of code works fine in a Swift Playgound and with any iOS8 or 8.1 simulator.以下代码在 Swift Playgound 和任何 iOS8 或 8.1 模拟器中都可以正常工作。 But with iOS7 and 7.1, the NSDate object is always set to nil.但是在 iOS7 和 7.1 中,NSDate 对象总是设置为 nil。
The object dateString contains a JSON string (ISO 8601 format) like 2015-02-28T20:15:00+0100 I'm trying to convert this date string into a NSDate object with the following code :对象 dateString 包含一个 JSON 字符串(ISO 8601 格式),如2015-02-28T20:15:00+0100我正在尝试使用以下代码将此日期字符串转换为 NSDate 对象:
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.ZZZ"
if let dateString = (json as NSDictionary).valueForKey("dateAndTime") as? String
{
let dateObject = dateFormatter.dateFromString(dateString)
}
Where's my mistake ?我的错在哪里? I'm getting confused!我开始糊涂了! Many thanks非常感谢
The problem is with your time zone offset, your date format is not proper for that.问题在于您的时区偏移量,您的日期格式不适合。 Use the following format,使用以下格式,
"yyyy-MM-dd'T'HH:mm:ssZZZZZ"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.