Search code examples
objective-cioscocoa-touchlocalization

How do I get the name of a day of the week in the user's locale?


I have a number between 1 and 7, which I want to turn into the user's locale's equivalent of Monday to Sunday. Can I do that and if so, how?


Solution

  • An NSDateFormatter can give you the list of names:

    NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setLocale: [NSLocale currentLocale]];
    NSArray * weekdays = [df weekdaySymbols];
    

    Which you can then index like any other array [weekdays objectAtIndex:dayIdx]; Be aware, however, that the first weekday may differ by locale; exactly how it may vary (along with many other things about NSCalendar) is not particularly well-explained in the docs.