Search code examples
objective-cnsdateiso

How to parse ISO 8601 date in objective-c


I need to create an NSDate from json, and the date in in a format I have never used. The date string I'm getting back is 2014-02-07T03:10:43.824Z. Does anyone know the correct date format string I need to use for the date formatter?


Solution

  •  NSDateFormatter *dateFormat = [NSDateFormatter new];
     //correcting format to include seconds and decimal place
     NSString* input = @"2014-02-07T03:10:59:434Z";
     dateFormat.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
     // Always use this locale when parsing fixed format date strings
     NSLocale* posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
     dateFormat.locale = posix;
     NSDate* output = [dateFormat dateFromString:input];