I'm am trying to convert a date string to NSDate format, but I'm not getting the correct results. I want NSDateFormatter to ignore everything to do with timezones and just use the Europe/Amsterdam timezone, regardless of the timezone set int the date string, since it's going to be an app for people within this timezone only.
My script:
func convertDate(dateStr: String) -> NSDate?{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.timeZone = NSTimeZone(name: "Europe/Amsterdam")
var date = dateFormatter.dateFromString(dateStr)
if (date != nil) {
return date
} else {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "Europe/Amsterdam")
date = dateFormatter.dateFromString(dateStr)
if (date != nil) {
return date
}
}
return nil
}
I use this function to convert a date to NSDate format, the date can be 2 formats, either the first one or the last one, so that's why I implemented this simple check.
Input string: 2015-09-07T12:48:00+0200
Output date: 2015-09-07 10:48:00 +0000Input string: 2015-09-07T10:35:00
Output date: 2015-09-07 08:35:00 +0000Input string: 2015-09-07T14:40:00
Output date: 2015-09-07 12:40:00 +0000
The dates are always 2 hours behind for some reason.