1

I'm using NSDateFormatter to format dates to relative dates. In Simulator, the dates appear correctly as "November 12, 2012", however on my device the same date appears as "2012-11-12". I'm running the same OS version both.

Here's the code I am using:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSString *dateString = [dateFormatter stringFromDate:dateToDisplay];

Thanks!

runmad
  • 14,846
  • 9
  • 99
  • 140
  • Yep, set the locale if you're not explicitly setting the date format. And even (especially) if you do explicitly set the format you need to beware of the [12/24 hour "feature"](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature). – Hot Licks Jul 15 '13 at 02:49

1 Answers1

6

Probably different localization. Check Settings --> General --> International on both your device and simulator. You can set the locale of your dateFormatter like

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en/US"];
[dateFormatter setLocale:locale];
jimpic
  • 5,360
  • 2
  • 28
  • 37
  • OK, thanks, that seems to be the issue alright. However, if I force `en/US` wouldn't that return the correct translation for dates? If the OS language is set to French, I'd prefer not display the English month or way of displaying the dates.. Any idea? – runmad Nov 12 '12 at 16:26
  • 1
    @runmad Do not set the locale on the date formatter unless you have a specific reason to format the date in that locale. By default, a date formatter uses the current locale based on the Region Format setting in the Settings app. – rmaddy Nov 12 '12 at 17:05
  • 1
    Alright, thanks for the clarification. Odd that Canadian English default to "yyyy-MM-dd" instead of relative dates. – runmad Nov 12 '12 at 18:35