Mark Struzinski
Mark Struzinski

Reputation: 33471

Objective-C NSDate - How to Get the Current Date From a Different Time Zone

I've been browsing the Apple docs for a while now, but haven't seen a way to get the current Date from a different time zone. Basically, I need to get the current date in US Central Standard Time (CST). I'm pretty sure it's a combination of NSDate and NSDateFormatter by setting the time zone. I'm sure this is an easy task, but I haven't been able to figure out the best way to do this.

Is there something I'm missing here?

Upvotes: 1

Views: 10859

Answers (1)

Vladimir
Vladimir

Reputation: 170829

If you want just display current time for a given zone you need to set NSDateFormatter's timeZone property:

[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];

If you want an NSDate object containing time from a given zone you need to set default time zone before retrieving that date:

NSDate* date = [NSDate date];
NSLog(@"%@", [date description]);

[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];
NSDate* nDate = [NSDate date];
NSLog(@"%@", [nDate description]);

//output
2010-08-05 12:32:38 +0300
2010-08-05 04:32:38 -0500

Upvotes: 3

Related Questions