1

I can't figure out why the next piece of code only works when I set the Region Format to United States.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *date = [dateFormatter dateFromString:currentFeedPubDate];
NSLog(@"%@", date);

Otherwise it returns "(null)".

Can anyone help me find out why ?

Titouan de Bailleul
  • 12,920
  • 11
  • 66
  • 121

3 Answers3

2

What is your currentFeedPubDate? Probably it is something like Sun, 15 Jun 2012 22:31:30 GMT+1, right?

Now, note that while in United States locale MMM will be something like Jan (from English January), in Polish locale it will be Sty (from Polish Styczeń, name for January) and thus your string won't match.

Krizz
  • 11,362
  • 1
  • 30
  • 43
2

As Krizz already noted, the issue is that the localized names won't match. For example, if you feed it a date like Sun, 15 Jan 2012 22:52:00 GMT+1 and the phone is currently set to German, it won't recognize Sun as it would look for an abbreviation of a German weekday name but won't find one.

If you know in which locale format the input string will be you can (and need to) force the locale on your date formatter like this:

[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

Additionally, there is a problem you may encounter if someone toggles the 12/24 setting in their phone's preferences. This problem is discussed in this thread.

Community
  • 1
  • 1
Hot Licks
  • 47,103
  • 17
  • 93
  • 151