views:

108

answers:

1

I have a very strange date format coming to me via JSON. e.g. - "July, 18 2010 02:22:09"

These dates are always UTC. I'm parsing the date with NSDateFormatter, and setting the timeZone to UTC...

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[inputFormatter setDateFormat:@"MMMM, dd yyyy HH:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:dtStr];

However, the date when logged is appearing with the offset of my device...

"2010-07-18 02:22:09 -0600"

What am I doing wrong here?

A: 

I think this is because your NSDate isn't using the UTC timezone. The docs say this about the description method of NSDate:

A string representation of the receiver in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”).

So, you aren't really doing anything wrong. NSDate is behaving as expected. What are you trying to accomplish? Do you need the date in a string? Are you doing date comparison?

Jergason
Thanks. You're correct. It's just confusing to have it showing an offset when NSDate doesn't actually store timezone info.
E-Madd
Not just time zone info, but format. It has no idea how it was created; it doesn't know that it was created from a US long-format date, nor that it was created with the time in UTC. That's why the description is in local time and ISO 8601 format: That's how every date describes itself, regardless of how it was created. To get the date represented in a specific format and/or in a specific time zone, you need to use a formatter to represent the date, the reverse of how you used a formatter to parse the date.
Peter Hosey