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,063
  • 1
  • 28
  • 42
0xSina
  • 19,923
  • 31
  • 130
  • 250

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
  • 91,777
  • 29
  • 160
  • 208
0

Try this instead:

[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
Abdullah Shafique
  • 6,850
  • 8
  • 31
  • 68
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,703
  • 3
  • 41
  • 62
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,190
  • 2
  • 41
  • 60