I found something that could solve my problem in Obj-c, but I'm not able to translate the code, so I'm asking for a Swift solution.
I'm parsing some data from a JSON file and I get an issue retrieving the date; here is the code :
println(data) // "Fri, 16 Jan 2015 11:49:00 +0100"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd LLL yyyy HH:mm:ss ZZZ"
let formattedDate = dateFormatter.dateFromString(data)
println(formattedDate) // returns 'Optional(2015-01-16 11:49:00 +0100)' if running on an iOs Simulator
// returns 'nil' if runnig on an iPhone
Like I write in the code comments, I get correctly the optional type of the date if I run it on an iOs Simulator or in the playground, but I get nil if it is running on an iPhone.
Can anyone help?
For the others who get here and don't read the comments of the initial post ;)
As @rintaro pointed out - and what was my problem too - you have to add a locale when you use fixed-format dates:
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
So in Swift 3 you would have to add
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
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.