1

I have this date:

2013-07-15T06:07:53-04:00

I use NSDateFormatter to convert this to NSDate:

    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];

This works but it doesn't recognize the timezone. It returns:

2013-07-15 10:07:53 +0000

What am i doing wrong?

Thanks

Marcel
  • 6,393
  • 1
  • 30
  • 44
0xSina
  • 20,973
  • 34
  • 136
  • 253

4 Answers4

2

The log output is correct. NSLogging an NSDate will always show that date/time in UTC (see how your output has +0000 instead of -04:00). NSDate objects represent a specific moment in time regardless of calendar and timezones. You use this "moment in time" in conjunction with a formatter (with a timezone set) in order to format that "moment in time" into a locale-specific time.

If you want to see log the output with your own timezone:

// pick a different timezone if necessary
[formatter setTimeZone:[NSTimeZone localTimeZone]];

NSLog(@"%@", [formatter stringFromDate:momentInTime];
dreamlax
  • 93,976
  • 29
  • 161
  • 209
0

Try this instead:

[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
0

isn't the "-04:00" at the end of the string telling NSDateFormatter that this date is GMT-4 and thus, it's just converting it to GMT ?

If not, please provide the full initialization of your NSDateFormatter.

teriiehina
  • 4,741
  • 3
  • 41
  • 63
0

more info here

iPhone NSDateFormatter Timezone Conversion

5 ZZZZZ - heres a category I wrote with some sample of GMT to BST

https://github.com/clearbrian/NSDateFormatter_ISO_8601

Community
  • 1
  • 1
brian.clear
  • 5,277
  • 2
  • 41
  • 62