3

I have an iOS app which needs to set a few different date labels according to the current day. I am using NSDate and NSDateFormatter to do this. However there is something I am not sure about: if the user has an iOS device with their language/localisation set to something other than English, then will my if statements that check to see if it is currently "Monday" or "Tuesday", stop working?

Here is my code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyyMMdd";
NSDate *date = [NSDate date];

dateFormatter.dateFormat = @"EEEE";
NSString *dayString = [[dateFormatter stringFromDate:date] capitalizedString];
NSLog(@"Day: %@", dayString);


if ([dayString isEqualToString:@"Monday"]) {

}

else if ([dayString isEqualToString:@"Tuesday"]) {

}

else if ([dayString isEqualToString:@"Wednesday"]) {

}

else if ([dayString isEqualToString:@"Thursday"]) {

}

else if ([dayString isEqualToString:@"Friday"]) {

}

else if ([dayString isEqualToString:@"Saturday"]) {

}

else if ([dayString isEqualToString:@"Sunday"]) {

}
jscs
  • 63,694
  • 13
  • 151
  • 195
Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
  • 1
    http://stackoverflow.com/questions/9874503/how-do-i-get-the-day-of-the-week-using-nsdate-and-show-using-nslog-in-ios don't compare strings. not everyone in the world has "Monday"... Montag? Lundi? Lunes? – Marc B May 20 '15 at 21:45
  • 1
    or at least set the locale of the formatter to en_US_POSIX – Matthias Bauch May 20 '15 at 21:48
  • @MarcB Your comment is the basis of the question. The OP is asking about non-English locales and the fact that the weekday names will be different. – rmaddy May 20 '15 at 22:09

3 Answers3

19

You can use following program.

NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];

switch ([component weekday]) {
    case 1:
        //Sunday
        break;
    case 2:
        //Monday
        break;
    ...
    case 7:
        //Saturday
        break;
    default:
        break;
}
deoKasuhal
  • 2,867
  • 20
  • 27
  • 1
    Careful because depending on the locale, Monday can be the first week day. – gnasher729 May 21 '15 at 00:27
  • 1
    Doesn't matter. If Monday is the first day of the week its index will still be 2(!). That being said. The indexes are wrong. See [documentation of NSCalendarUnitWeekDay](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/index.html#//apple_ref/c/tdef/NSCalendarUnit) **The weekday units are the numbers 1 through N (where for the Gregorian calendar N=7 and 1 is Sunday).** – Matthias Bauch May 21 '15 at 07:43
  • @rmaddy answer indexes Sunday as a 0. But your answer thinks of 1 as a Sunday. So which one is it? – Supertecnoboff May 22 '15 at 08:25
  • 1
    @Supertecnoboff This answer and mine are both correct. The `NSDateComponents weekday` method returns a value in the range 1-7 with Sunday being 1. In my answer, I'm getting the index of a an array which will have a range of 0-6 with Sunday being 0. They are two completely different approaches so each much be handled accordingly. – rmaddy May 22 '15 at 14:52
5

While the answer using NSDateComponents is the best option, another possibility that works with the weekday string would be:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"EEEE";
NSDate *date = [NSDate date];
NSString *dayString = [dateFormatter stringFromDate:date];

NSInteger weekdayNum = [[dateFormatter weekdaySymbols] indexOfObject:dayString];
switch (weekdayNum) {
    case 0:
        //Sunday
        break;
    case 1:
        //Monday
        break;
    ...
    case 6:
        //Saturday
        break;
    default:
        break;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    Yes. The `NSDateFormatter weekdaySymbols` (and all the other symbols in the class) will be appropriate to whatever locale is set on the date formatter (which defaults to the user's current locale). But keep in mind that I pointed this out as a 2nd rate solution. The other answer, using `NSDateComponents` is far more efficient than doing all of the date formatting and string lookups. – rmaddy May 20 '15 at 22:04
2

On you Swift, you can do this:

let calendar: Calendar = Calendar.current
let isMonday = calendar.component(Calendar.Component.weekday, from: self) == 2

Or you can use this Date extension to get the week day of a date:

extension Date {

    enum WeekDay: Int {
        case sunday = 1
        case monday
        case tuesday
        case wednesday
        case thursday
        case friday
        case saturday
    }

    func getWeekDay() -> WeekDay {
        let calendar = Calendar.current
        let weekDay = calendar.component(Calendar.Component.weekday, from: self)
        return WeekDay(rawValue: weekDay)!
    }
}

let date = Date()
date.getWeekDay()
pableiros
  • 14,932
  • 12
  • 99
  • 105