Swift ISO-8601 date formatting with iOS7

試著忘記壹切 提交于 2019-12-13 14:28:25

问题


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. But with iOS7 and 7.1, the NSDate object is always set to 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 :

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


回答1:


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"


来源:https://stackoverflow.com/questions/28791771/swift-iso-8601-date-formatting-with-ios7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!