-1

I have a simple question but it is driving me nuts. I am using an API, and I get a JSon object back, and one of the fields is a date. The date is formatted like this: 2013-05-17T02:00:00.000Z. I cannot seem to get the NSDateformatter correct for this date. Would anyone be willing to give me a hand?

If it is not clear, I am using objective c for an iPhone app. My goal is to get a NSDate object at the end of this. Thanks again for your help.

Becca Royal-Gordon
  • 17,541
  • 7
  • 56
  • 91
Samuel Rosen
  • 183
  • 1
  • 2
  • 5

2 Answers2

2

That's one of the ISO 8601 formats. Parsing it with NSDateFormatter can be tricky at best, which is why I wrote an NSFormatter subclass specifically for parsing and unparsing any ISO 8601 format.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
1

You'll want to get the correct format for that string. Then you should be able to do something like:

NSString * dateString = jsonDict[@"dateKey"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]
NSDate *date = [df dateFromString:dateString];

The T is in there to separate date from time and the Z specifies Zulu time. (https://stackoverflow.com/a/8405125/1074558)

Community
  • 1
  • 1
atreat
  • 4,243
  • 1
  • 30
  • 34