When you set the dateFormat line, you must also set the locale property to what is compatible with the format provided. Otherwise, the locale will be based on device settings, which may be incompatible.
The date format specified here will work with the "en" locale, but it will not work with many others, such as "eu". Here is an example:
let dateString = "2014-06-21 14:56:00 EST" let localeStr = "eu" // this will give nil let localeStr = "us" // this will succeed let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: localeStr) dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz" var date: NSDate? = dateFormatter.dateFromString(dateString)
The problem here is EST, which only exists in North America. In all other places, this is the wrong time zone. If you change the date string to this:
"2014-06-21 14:56:00 UTC-5"
It will then be formatted correctly, regardless of what locale value is set.
Abhi beckert
source share