I'm trying to convert string to NSDate. I suppose that my dateFormat is ok, but i still receive nil. My code:
let dateStr = tweet.objectForKey("created_at") as! String
print(dateStr)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
let date = dateFormatter.dateFromString(dateStr)
print(date)
Output:
Mon Apr 04 20:12:54 +0000 2016
nil
Mon Apr 04 14:17:09 +0000 2016
nil
Mon Apr 04 14:07:32 +0000 2016
nil
Sun Feb 21 12:37:23 +0000 2016
nil
I am using the same code and it's working for me. Assuming, you are getting correct value for dateStr
as it is shown in Output.
let dateStr = "Mon Apr 04 20:12:54 +0000 201"
print(dateStr)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
let date = dateFormatter.dateFromString(dateStr)
print(date!)
Output:
Mon Apr 04 20:12:54 +0000 2016
2016-04-04 20:12:54 +0000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.