22

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?

1
  • 9
    (a) be nice. (b) that's getting it from a date, not a day number.
    – Simon
    Sep 7, 2011 at 8:52

7 Answers 7

28

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.

2
  • 3
    FYI - according to the iOS 7.1 to iOS 8.0 API diffs, (but not the actual reference docs), the weekdaySymbols method was deprecated from NSDateFormatter and made a class method of the NSCalendar class.
    – rmaddy
    Oct 3, 2014 at 4:46
  • 1
    Weird place to put it. Not really an NSCalendar's job, I'd say, and certainly not the class's. Now you have to go rooting around in an NSLocale to get the symbols for a locale other than the device's? Thanks for the note, though.
    – jscs
    Oct 3, 2014 at 5:42
19

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"]
1
  • 1
    i was looking for veryShortWeekdaySymbols ...(: Thanks..dude
    – guru
    Sep 18, 2018 at 10:28
11

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]
}
0
2
NSArray *weekdaySymbols = [[NSDateFormatter alloc] weekdaySymbols];

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

2
  • [NSLocale currentLocale] is not needed.
    – ChangUZ
    Sep 8, 2011 at 5:20
  • 6
    ...but you should probably initialize your NSDateFormatter before calling weekdaySymbols... [[[NSDateFormatter alloc] init] weekdaySymbols] :) May 6, 2012 at 7:18
2

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
}
1
  • 1
    Awesome answer I was looking for. Thank you Jul 25, 2020 at 15:40
0

I found this solution: You have your date object, you want to get the day of the week out of that object.

  1. Set the locale to your date formatter as show below

    let eventDate = dateFormatter.date(from: self)
    let language = Locale.preferredLanguages.first
    print("The locale id is  \(language)")
    // This date format can be what ever you like, in this case i used EE dd
    dateFormatter.dateFormat = "EE dd"
    if let idiomaLocale = language {
        dateFormatter.locale = Locale(identifier: idiomaLocale)
    } else {
        dateFormatter.locale = Locale(identifier: "es_CO")
    }
    
  2. Doing this you can be sure that you are getting the correct identifier for your date formatter, after that with your date formatter configured you can proceed and get the string of the day of week

    //convert your date to string
    let stringDate = dateFormatter.string(from: eventDate!)
    
    return stringDate
    

I hope this helps somebody :). happy new year.

2
  • Thanks for the new year wishes! My question was how to get a name from a numbered day of the week, not from a date.
    – Simon
    Jan 1, 2018 at 11:40
  • Ok may be i misunderstood the question Jan 3, 2018 at 18:00
0

If you are dealing with Date objects, assuming that you have added the localization option to your project, you can easily use the following code:

extension Date {
    var dayName: String {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: Locale.current.identifier)
        dateFormatter.dateFormat  = "E" // "EEEE" to get long style
        return dateFormatter.string(from: self)
    }
}

This will automatically detect your app's language and output the localized day as a string.

2
  • Thanks, but as per the question, I don't have a date object, I have a number between 1 and 7.
    – Simon
    Jun 12, 2021 at 15:31
  • 2
    I know :), but since the title doesn't mention the object type, many people (including me) will land on this page even though they are dealing with a date object. Since your question is already answered for numbers, I wrote how the solution would be for a date object, to help those who get to this page because of your title and the google search result.
    – Khashayar
    Jun 12, 2021 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.