16

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.

Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
  • When you talk about iOS output, do you mean NSLog? If so, NSLog always default the output the GMT. So, there is nothing wrong with your code. – Black Frog Apr 05 '11 at 23:54
  • Yes, that's what I was referring to. But if `NSLog` outputs in GMT what does say a `UILabel` output? – Gup3rSuR4c Apr 05 '11 at 23:59
  • NSDate stores the time as GMT. You will have to use [NSDateFormatter](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html) -stringFromDate method when displaying any date to string. NSDateFormatter default to the current time zone also. – Black Frog Apr 06 '11 at 00:13
  • Also consider setting the `locale` as suggested in [Apple Technical Q&A 1480](https://developer.apple.com/library/ios/qa/qa1480/_index.html). – Rob Oct 22 '14 at 14:43

3 Answers3

14

I don't know how right this is, but I ultimately found that in .NET I have to do DateTime.ToUniversalTime().ToString("yyyy-MM-ddHH:mm:ss") and on the iOS side I have to do this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2011-04-0600:28:27"];

Only then is the date correct when NSLog outputs, so I'm assuming that I've finally got a proper date/time.

Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
6

Your iOS date parsing code is correct: 2011-04-05 16:28:22 -07:00 and 2011-04-05 23:28:22 +0000 represent the same time, just in different time zones. Your only problem is that NSDate doesn't actually store the time zone, and so [date description] outputs using UTC.

Anomie
  • 92,546
  • 13
  • 126
  • 145
2

You're almost certainly better off just using UTC time everywhere for interchange and not bothering with time zones. I'm sure you can get that your ASP.NET code to do that, and it's easier to parse on the receiving end as well. NSDateFormatter uses standard Unicode date formatting syntax, which you can read about here.

Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
  • I see, I've changed the .NET side to output in Universal time, but I'm not sure how to parse the string on the iOS side. Here's the JSON response: `{"Start":"\/Date(1302048496487)\/","End":"\/Date(1302055696487)\/"}`. What would be the proper code to parse that? – Gup3rSuR4c Apr 06 '11 at 00:10
  • Unfortunately, disregarding timezones, while nice for a computer, is not "entirely correct" for a human. Daylight savings, etc, make this even more of a chore :( –  Dec 30 '11 at 06:31
  • I didn't mean in general, I meant on the server. Believe me, I write an alarm app and have spent way too much time dealing with time zones and formstting. :-) – Nicholas Riley Dec 30 '11 at 15:27
  • @NicholasRiley Just keeping you on your toes ;-) (I'm currently in a timezone battle that I don't think I'll win...) –  Dec 30 '11 at 22:12