1

I have a string that is UTC and would like to convert it to an NSDate.

static NSDateFormatter* _twitter_dateFormatter;
[_twitter_dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[_twitter_dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
[_twitter_dateFormatter setLocale:_en_us_locale];

NSDate *d = [_twitter_dateFormatter dateFromString:sDate];

When I go through the debugger d is nil even though sDate is "2010-03-24T02:35:57Z"

Not exactly sure what I'm doing wrong.

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • Also just a little nit-pick: d is nil in the debugger not *d (you can't dereference a null pointer). – Alan Rogers Mar 24 '10 at 02:46
  • Actually, your string is not "UTC". Your 'sDate' string value is the standard format (RFC 3339 or ISO 8601) representing a datetime that happens to be in the Zulu (UTC) time zone (the 'Z' on the end). – Basil Bourque Sep 29 '12 at 21:32

3 Answers3

3

Ditto on the need to alloc/init your NSDateFormatter object, but you have another problem too: your Date format string does not match the actual date you're giving it.

"EEE MMM dd HH:mm:ss ZZZ yyyy"
- vs -
"2010-03-24T02:35:57Z"

That format string would match something like:

"Wed Mar 24 00:07:33 -0400 2010"

See the unicode standard for the meaning of the date format string.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • Thanks for pointing out the mismatch. I'm not sure how I can format the string to be like "2010-03-24T02:35:57Z" – Sheehan Alam Mar 24 '10 at 05:06
  • Similar question http://stackoverflow.com/questions/1263455/nil-nsdate-when-trying-to-get-date-from-utc-string-in-zulu-time In particular, note the suggestion that you may want to replace the final "Z" in the input string with an explicit numeric timezone (-0000) – David Gelhar Mar 24 '10 at 05:14
  • Thanks! In my case the date format should be yyyy-MM-dd'T'HH:mm:ssZ Replacing Z with (-0000) did the trick. – Sheehan Alam Mar 24 '10 at 05:31
1

You aren't allocating or initialising your NSDateFormatter object.

Try something like this:

NSDateFormatter* _twitter_dateFormatter = [[NSDateFormatter alloc] init];
[_twitter_dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[_twitter_dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
[_twitter_dateFormatter setLocale:_en_us_locale];

NSDate *d = [_twitter_dateFormatter dateFromString:sDate];
[_twitter_dateFormatter release];

It's also unclear why you are declaring _twitter_dateFormatter as a static. If you are trying to avoid re-allocating it, make it an ivar of your class.

Alan Rogers
  • 15,018
  • 3
  • 28
  • 19
0

Apple gives source code for Parsing an RFC 3339 date-time. If using ARC, you'll may want to adjust that source by removing the calls to 'autorelease'.

FYI, RFC 3339 is a subset of ISO 8601 with one deviation (negative zero offset is allowed in RFC 3339). So Apple’s code is killing two birds with one stone, handling the so-called 'Internet-style' RFC 3339 datetimes as well as simple datetimes of ISO 8601.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154