Reputation:
i'm using xCode 7.2.1 ad Swift2, writing fo iOS.
i did a function for converting a String to a NSDate and that's :
class func StringToDateWithHour(data : String) -> NSDate
{
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "CET");
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let date = dateFormatter.dateFromString(data) else{
return NSDate()
}
return date
}
I don't understand why i get back the UTC time, INSTEAD of CET time. When i try to convert a string like : "2016-2-21 10:00:00" i get back "2016-2-21 09:00:00" , one hour less. Thank you.
Upvotes: 0
Views: 1128
Reputation: 2600
You're saying that you want to convert 10 am CET to a NSDate. That correctly outputs 9 am UTC time (UTC is one hour behind CET time).
NSDate objects don't depend on time zones, they're just data from a particular point in time.
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
Upvotes: 0