0

I need to check on dates for certain items. I'm reading a string from a JSON file that looks like:

"revisionDate":"2013-08-28T13:07:53+00:00" 

When I try to format it

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-d HH:mm:ssZ"];
NSDate *date = [dateFormat dateFromString:revisionDateTime];

date is always nil. I think this is because of that T that's in that timestamp string. I don't know how to handle it, and there doesn't seem to be any mention how (at least that I can find), in the reference I'm using.

Any ideas on how I can handle this?

jscs
  • 63,694
  • 13
  • 151
  • 195
easythrees
  • 1,530
  • 3
  • 16
  • 43
  • Can you confirm that `revisionDate` is an `NSString`? – zeantsoi Jan 07 '14 at 05:35
  • It's not just the "T", it's the time zone offset bit too. Possible duplicate of [Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?](http://stackoverflow.com/questions/5185230/converting-an-iso-8601-timestamp-into-an-nsdate-how-does-one-deal-with-the-utc) – jscs Jan 07 '14 at 05:37

1 Answers1

2

Try modifying the NSDateFormatter instance to account for the T:

[dateFormat setDateFormat:@"yyyy-MM-dd T HH:mm:ssZZZZ"];

You may also want to consider encapsulating literal string values (in this case, dashes, spaces, colons, and the T) between quotes:

[dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZ"]
zeantsoi
  • 25,857
  • 7
  • 69
  • 61