-1

Possible Duplicate:
Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

i'm trying to convert date time which is in iso 8601 format int local time. convert this:

2012-10-15T09:46:16+02:00

to this:

2012-10-15 11:46:16

i try the following code but gives null back

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *dt = [dateFormatter dateFromString:@"2012-10-15T09:46:16+02:00"];
NSString *dateTime= [NSDateFormatter localizedStringFromDate:dt dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];
NSLog(@"DATE FORMAT:%@", dateTime);

any idea what am i doing wrong?

Community
  • 1
  • 1
Hashmat Khalil
  • 1,826
  • 1
  • 25
  • 49

1 Answers1

2

You need to use ISO8601DateFormatter. It's a class designed for this. The Date format you are working with is not restricted to just one format for the NSDateFormatter date format. The ISO8601DateFormatter already identifies and parses the date format as long as it is ISO8601. http://boredzo.org/iso8601unparser/

J2theC
  • 4,412
  • 1
  • 12
  • 14
  • Library looks great, but it's not currently compatible with ARC: https://github.com/boredzo/iso-8601-date-formatter/pull/1 – bdalziel Mar 26 '14 at 18:18
  • 1
    You can set a compiler flag to tell the compiler not to use arc on that specific file. – J2theC Mar 26 '14 at 18:46
  • great to know, thanks so much. Looks like this will do it: http://stackoverflow.com/a/8040712/822164 – bdalziel Mar 26 '14 at 19:28