Convert GMT NSDate to device's current Time Zone

ios, iphone, objective-c, parse-platform

Solution

NSDate is always represented in GMT. It's just how you represent it that may change.

If you want to print the date to `label.text`, then convert it to a string using `NSDateFormatter` and `[NSTimeZone localTimeZone]`, as follows:

NSString *gmtDateString = @"08/12/2013 21:01";

NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"dd/MM/yyyy HH:mm"];

//Create the date assuming the given string is in GMT
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [df dateFromString:gmtDateString];

//Create a date string in the local timezone
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [df stringFromDate:date];
NSLog(@"date = %@", localDateString);

// My local timezone is: Europe/London (GMT+01:00) offset 3600 (Daylight)
// prints out: date = 08/12/2013 22:01

Problem

I'm using Parse.com to store some values: These are GMT values. How do I convert these to the device's current time zone and get NSDate as a result?

Original source