6

I have several date strings that I need to convert to NSDates. My parsing code is the following:

NSString *s = [pair objectForKey:@"nodeContent"];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZ"];
self.date = [f dateFromString:s];

The above code works fine in all the devices and simulators we've tested. The strange thing is that when the above code runs on any iPhone 5 running iOS 6.1 or 7.0.x , the line self.date = [f dateFromString:s]; returns nil every time.

I have checked and the string s exists and contains the same characters when compared side-by-side with a device that does parse the date correctly.

This is an example date string: `2013-10-31T21:50:00-06:00'

Am I missing something here?

fabian789
  • 8,348
  • 4
  • 45
  • 91
  • quantify *any*. Could it be an issue with the locale? – Gabriele Petronella Oct 24 '13 at 23:23
  • possible duplicate of [What is the best way to deal with the NSDateFormatter locale "feature"?](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature) – Hot Licks Mar 31 '14 at 18:50

1 Answers1

13

It's because you are not setting the date formatter's locale to the special en_US_POSIX locale. Most likely your iPhone 5 has a different setting for the 24-hour setting.

You need to set the special locale whenever you parse a fixed format string.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 3
    Thank you. I added the line `[f setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];` and it works now. – user2917941 Oct 24 '13 at 23:35