Andrew Smith
Andrew Smith

Reputation: 2929

NSDate / NSDateFormatter returning GMT on iPhone iOS 4.3

Why would this code be giving me GMT? (I am in US Mountain Time)

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];     

NSDate *now = [NSDate date];
NSString *storeTime = [dateFormatter stringFromDate:now];

Upvotes: 1

Views: 1719

Answers (2)

Andrew Smith
Andrew Smith

Reputation: 2929

Yes, turns out this was a bug in Apple's Numbers on the Mac. Numbers was not interpreting the date string, or rather it was adding the time offset. The NSDate string, after formatting, was correct all along.

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243166

An NSDate represents a concrete point in time, regardless of the timezone. Put another way, an NSDate does not have a timezone. Timezones are only relevant when you want to display the date to the user. So 9:30pm in Mountain Time is 3:30am (+1 day) in GMT (assuming a 6 hour time difference).

NSDate, since it does not have a timezone, must pick one when producing a human-readable version to return as its -description. To make things simple, it always returns a date formatted in the GMT time zone. If you would like the date formatted to be in a different timezone, you can set the -timezone property of an NSDateFormatter, and then convert the date into a string using the -stringFromDate: method.

Upvotes: 3

Related Questions