Question

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?

Was it helpful?

Solution

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"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top