4

I'm trying to get the localized weekday from an nsdate object

+ (NSString *)localizedWeekdayForDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
    dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:[NSLocale localeWithLocaleIdentifier:language]];
    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    return formattedDateString;
}

The language string is always "en" ... even thou the device language is not english... I tried [NSLocale currentLocale]; as well as preferedLanguages... this also doesn't work..

Any suggestions?

user1028028
  • 6,323
  • 9
  • 34
  • 59
  • 1
    I think you are confusing the language with the locale. Binding the locale to the device language like that is a bad idea. For example; my devices are set to English but my region formats (locale) are Turkish. With your code I will always have USA date formats. – Desdenova Dec 31 '13 at 14:15
  • Correct... I'm using the regons format now – user1028028 Dec 31 '13 at 14:44

2 Answers2

6
[NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:[NSLocale localeWithLocaleIdentifier:language]]

Does not set the locale, it only returns a NSString. The locale needs to be set with:

- (void)setLocale:(NSLocale *)locale

Examples:

ObjectiveC

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"fr"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"EEEE"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: '%@'", formattedDateString);

NSLog output:

formattedDateString: 'mardi'

Swift 3

let date = Date()
let dateFormatter = DateFormatter()
let locale = Locale(identifier:"fr")
dateFormatter.locale = locale
dateFormatter.dateFormat = "EEEE"
let formattedDateString = dateFormatter.string(from:date)
print("formattedDateString: \(formattedDateString)")

Output:

formattedDateString: 'mardi'

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Using `dateFormatter.dateFormat` will set the date format correctly. – rckoenes Dec 31 '13 at 13:55
  • OK. Note the OP's `dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:currentLocale];' did not set the locale as the OP expected. I wrote that badly, I will change that. In your line – zaph Dec 31 '13 at 14:08
0

You are forgetting to set the local of the dateFormatter:

+ (NSString *)localizedWeekdayForDate:(NSDate *)date
{
    NSLocale *currentLocale = [NSLocale currentLocale];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.locale = currentLocale;
    dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:currentLocale];

    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    return formattedDateString;
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166