Does the weekdaySymbol list always start with Sunday in any country?
Asked Answered
L

4

11

I have this code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSMutableArray *daysNames = [NSMutableArray arrayWithArray:dateFormatter.weekdaySymbols];
NSLog(@"daysNames = %@", daysNames);

it outputs:

daysNames = (
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
)

My question is:

If the user is in a country other than US, let's say France or Russia, will the array still start with Sunday (not Monday), or should I not rely on this?

The thing is I set alarm days. Visually, the user chooses from a table view, which always has Monday in the first row. And I keep 0 or 1 in an NSMutableArray based on the fact if the day is set or not. If daysNames[0] always corresponds to Sunday, I can easily shift all the elements one position to the right, and everything will map correctly, otherwise I have some more headaches dealing with one more case when week starts with Monday, not Sunday.

This is the full code I wrote for this (in United States it works perfectly):

// Set the short days names

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        NSMutableArray *daysNames = [NSMutableArray arrayWithArray:dateFormatter.weekdaySymbols];
        NSLog(@"daysNames = %@", daysNames);

        // daysNames will become @"SUN MON TUE WED THU FRI SAT";
        for (NSInteger i = 0; i < daysNames.count; i++) {

            NSUInteger length = ((NSString *)daysNames[i]).length;

            if (length > 3) {
                length = 3;
            }

            daysNames[i] = [daysNames[i] substringToIndex:length].uppercaseString;
        }

        NSString *sundayShortName = daysNames[0];

        // daysNames will become @"MON TUE WED THU FRI SAT SUN";
        for (NSInteger i = 1; i < daysNames.count; i++) {

            daysNames[i - 1] = daysNames[i];
        }

        daysNames[daysNames.count - 1] = sundayShortName;

        NSMutableArray *alarmDaysShortNames = [NSMutableArray array];

        for (NSInteger i = 0; i < alarm.alarmDays.count; i++) {

            if ([alarm.alarmDays[i] boolValue] == YES) {

                [alarmDaysShortNames addObject:daysNames[i]];
            }
        }

        alarmCell.alarmDaysLabel.text = [alarmDaysShortNames componentsJoinedByString:@" "];
Loginov answered 4/12, 2014 at 3:18 Comment(5)
No, in France, it would start with Dimanche. In addition, the Chinese have six or seven different words for Sunday so I'm not sure a simple array will cut it :-)Jokester
Thank you! I actually did not mean a different day name. I know that will get set based on language. I am wondering if daysNames[0] will still be Sunday(or Dimanche etc.) but not Monday(or Lunedi etc.).Loginov
My apologies, I misunderstood the question.Jokester
Should your application care which day the week starts? What is my week starts on Wednesday? Does it break your application? You should just provide a setting where the user can set which day of the week they want it to start on.Prady
The thing is I set alarm days. Visually, the user chooses from a table view, which always has Monday in the first row. And I keep 0 or 1 in an NSMutableArray based on the fact if the day is set or not. If daysNames[0] always corresponds to Sunday, I can easily shift all the elements one position to the right, and everything will map correctly, otherwise I have some more headaches dealing with one more case when week starts with Monday, not Sunday.Loginov
C
6

Yes. Sunday is always the weekday that comes first in that list. You can interrogate NSCalendar to find out what position in that list the week is considered to start on (Sunday for Americans, Monday for Europeans, etc).

Collings answered 4/12, 2014 at 3:30 Comment(3)
Thank you! You just said that "Sunday is always the weekday that comes first in that list.", but also that "Monday for Europeans". Can you check the full code I post and confirm that it is going to work in Europe and elsewhere or not? Thanks in advance!Loginov
Tommy, I tried to change the language on my iPhone to Russia and Romanian, and the code I added to the question seems to work fine. Can you just have a look over it and confirm that it would work? Thanks in advance!Loginov
@Loginov yes, that should be correct. The list will always be (localised versions of) "Sunday, Monday, Tuesday..." in that order. Which index is the first day of the week you get from elsewhere. In your case if you want to assume index 1 then your code is correct.Collings
M
9

Not sure if understood but here is the proposed solution, in Swift:

let shortWeekdaySymbols = Calendar.current.shortWeekdaySymbols

let localizedWeekdays: [String] = Array(shortWeekdaySymbols[Calendar.current.firstWeekday - 1 ..< Calendar.current.shortWeekdaySymbols.count] + shortWeekdaySymbols[0 ..< Calendar.current.firstWeekday - 1])

where localizedWeekdays is an array with a localized order of all the weekdays contained in Calendar's weekdaySymbols property.

Metachromatism answered 17/4, 2018 at 9:36 Comment(0)
C
6

Yes. Sunday is always the weekday that comes first in that list. You can interrogate NSCalendar to find out what position in that list the week is considered to start on (Sunday for Americans, Monday for Europeans, etc).

Collings answered 4/12, 2014 at 3:30 Comment(3)
Thank you! You just said that "Sunday is always the weekday that comes first in that list.", but also that "Monday for Europeans". Can you check the full code I post and confirm that it is going to work in Europe and elsewhere or not? Thanks in advance!Loginov
Tommy, I tried to change the language on my iPhone to Russia and Romanian, and the code I added to the question seems to work fine. Can you just have a look over it and confirm that it would work? Thanks in advance!Loginov
@Loginov yes, that should be correct. The list will always be (localised versions of) "Sunday, Monday, Tuesday..." in that order. Which index is the first day of the week you get from elsewhere. In your case if you want to assume index 1 then your code is correct.Collings
S
1

Yes, you should not rely on this. If you want to start your weekday from monday rather than sunday. You can try like this:-

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setFirstWeekday:2]; //it is for monday
Slumlord answered 4/12, 2014 at 3:28 Comment(0)
S
0

I just put my logic for Swift 4.

let dateFormatter = DateFormatter()
var weekDays: [String] = dateFormatter.weekdaySymbols
if weekDays[0] == "Sunday" {
     weekDays.append(weekDays[0])
     weekDays.remove(at: 0)
}
print(weekDays)

Check if first day is Sunday then first append "Sunday" in the Array list and remove it from first index.

Scuttlebutt answered 29/5, 2018 at 13:12 Comment(1)
it's not an universal solution, because of depending on Locale "Sunday" would change to different valueTarrel

© 2022 - 2024 — McMap. All rights reserved.