I have an ASP.NET MVC 3 website that communicates with my iOS app via JSON. As part of the objects sent in the JSON response, I have dates in the format of yyyy-MM-dd HH:mm:ss ZZZ
which outputs 2011-04-05 16:28:22 -07:00
. How do I parse that in iOS?
This is the code I'm messing around with right now:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:@"2011-04-05T16:28:22-0700"];
NSLog(@"%@; %@; %@", dateFormatter, date, [NSTimeZone localTimeZone]);
First thing to note is that 2011-04-05 16:28:22 -07:00
has to look like 2011-04-05T16:28:22-0700
, where a T
replaces the first space (assuming that stands for time, or where the time part of the string starts from?), the second space is removed and the colon in the time zone is removed. I figure I'll find a way to format the string that .NET is sending back to conform to the string iOS will parse.
The real issue is that the date that is outputted is 7 hours ahead of what I've sent in the JSON response. So, iOS outputs 2011-04-05 16:28:22 -07:00
as 2011-04-05 23:28:22 +0000
, and that is wrong as far as my app is concerned.
The only solution I've found so far is to send the date in the JSON as 2011-04-05 16:28:22 +00:00
, but that again is wrong because I'm altering what the real date should be.
Anyway, I'd appreciate someone taking a look and letting me know how I can parse the date string .NET is outputting via the format yyyy-MM-dd HH:mm:ss ZZZ
(which I suppose can be re-written to yyyy-MM-ddTHH:mm:ssZZZ
) to an NSDate
object I can use in iOS.