Question

I want to convert the current date into canada time.

It shows 1 hour late

For Eg

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a";
NSTimeZone *utc =[NSTimeZone timeZoneWithAbbreviation:@"UTC-08:00"];
[dateFormatter setTimeZone:utc];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];

NSLog(@"Time %@ ",timeStamp);

This returns

2014-05-01 21:17 PM

But the current time in canada is

2014-05-01 22:17 PM

I think i have to respond to Daylight saving. So how to get the time considering the DST

DST information to canada http://www.timeanddate.com/time/change/canada/vancouver

Answer:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a";
NSTimeZone *utc =[NSTimeZone timeZoneWithName:@"America/Vancouver"];
[dateFormatter setTimeZone:utc];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
Was it helpful?

Solution

You are specifying a UTC offset. Instead, you need to specify a timezone:

NSDateFormatter* df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone timeZoneWithName:@"__your timezone here__"]];

You can get the list of supported timezones as follows:

NSLog(@"%@", [NSTimeZone knownTimeZoneNames]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top