29

Not sure what the issue is, but instead of getting a month name "July", I get "07".

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];    
[dateFormat setDateFormat:@"MMMM dd, h:mm a"];

I have tried M, MM, MMM, MMMM, and all of them give me a number, instead of the month name, though with different amounts of leading 0s.

Chris
  • 1,421
  • 1
  • 15
  • 21
  • I think this line is the problem .. [dateFormat setLocale:[NSLocale systemLocale]]; don't know what is your purpose to use `systemLocale` but you can try `currentLocale` instead of `systemLocale` – ila Aug 24 '12 at 19:49

3 Answers3

54

Turns out to be an issue with the second line, setLocale. I assume that the system won't default to using english month names when the locale has been manually set? I live in an english speaking locale, but maybe it doesn't matter.

In any case, not setting the locale fixes the month name issue.

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];    
[dateFormat setDateFormat:@"MMMM dd, h:mm a"];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
Chris
  • 1,421
  • 1
  • 15
  • 21
15

The problem is the locale, not the format. Check this out:

http://waracle.net/mobile/iphone-nsdateformatter-date-formatting-table/

Basically, "MMMM" will give you the full name for the month.

J2theC
  • 4,412
  • 1
  • 12
  • 14
8

-systemLocale is not what you want here. It returns a fallback locale if no other locale fits. Use -currentLocale to get the user's current locale.

Also:
You should use NSDateFormatter's +dateFormatFromTemplate:options:locale: method to get the correct date format. In some languages the day comes before the month, etc. and vice versa.

Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60