Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 450
Simon Avatar asked Sep 07 '11 07:09

Simon


5 Answers

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.

like image 140
jscs Avatar answered Nov 15 '22 10:11

jscs


Here's my take:

Swift 3, iOS8+

func weekdayNameFrom(weekdayNumber: Int) -> String {
    let calendar = Calendar.current
    let dayIndex = ((weekdayNumber - 1) + (calendar.firstWeekday - 1)) % 7
    return calendar.weekdaySymbols[dayIndex]
}

Note that you have 3 lists available:

  • weekdaySymbols -> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
  • shortWeekdaySymbols -> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
  • veryShortWeekdaySymbols -> ["S", "M", "T", "W", "T", "F", "S"]
like image 22
Arnaud Avatar answered Nov 15 '22 11:11

Arnaud


Sure can. Below is a method / function that returns the weekday name for the weekday number in the range 0 to 6.

The Objective C version:

- (NSString *)weekdayNameFromWeekdayNumber:(NSInteger)weekdayNumber
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // Fetch the days of the week in words for the current language (Sunday to Saturday)
    NSArray *weekdaySymbols = calendar.weekdaySymbols;

    // Because the first week day changes depending on the region settings.
    //  ie. In Bangladesh the first day of the week is Friday. In UK it is Monday
    NSInteger index = (weekdayNumber + calendar.firstWeekday - 1) % 7;
    return weekdaySymbols[index];
}

and the Swift 2 version:

func weekdayNameFromWeekdayNumber(weekdayNumber: Int) -> String {
    let calendar = NSCalendar.currentCalendar()
    let weekdaySymbols = calendar.weekdaySymbols
    let index = (weekdayNumber + calendar.firstWeekday - 1) % 7
    return weekdaySymbols[index]
}

and Swift 3.0 version:

func weekdayNameFrom(weekdayNumber: Int) -> String {
    let calendar = Calendar.current
    let weekdaySymbols = calendar.weekdaySymbols
    let index = (weekdayNumber + calendar.firstWeekday - 1) % 7
    return weekdaySymbols[index]
}
like image 34
gavdotnet Avatar answered Nov 15 '22 10:11

gavdotnet


NSArray *weekdaySymbols = [[NSDateFormatter alloc] weekdaySymbols];

You can use one of {weekdaySymbols, shortWeekdaySymbols, veryShortWeekdaySymbols}

like image 2
ChangUZ Avatar answered Nov 15 '22 11:11

ChangUZ


Based on gavdotnet excellent solution, the European version (that starts from Monday) is:

func weekdayNameFromWeekdayNumber(weekdayNumber: Int) -> String {
    var EuroDay = weekdayNumber + 1
    if EuroDay == 7 {
        EuroDay = 0
    }
    let calendar = NSCalendar.currentCalendar()
    let weekdaySymbols = calendar.weekdaySymbols
    let index = EuroDay + calendar.firstWeekday - 1
    return weekdaySymbols[index].uppercaseString
}
like image 2
Blasco73 Avatar answered Nov 15 '22 11:11

Blasco73