Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

ios - NSCalendar, why does setting the firstWeekday doesn't effect calculation outcome?

i need to calculate the weekday for a given date, however, depending on the calendar a week can star on Monday and somwhere on Sunday

so i wanted to set it, to start on Monday, using

[[NSCalendar currentCalendar] setFirstWeekday:2];

however, the calculation outcome is the same

{
    [[NSCalendar currentCalendar] setFirstWeekday:1];
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    NSInteger weekday = [weekdayComponents weekday] - 1;
    NSLog(@"%d", weekday);
}
{
    [[NSCalendar currentCalendar] setFirstWeekday:2];
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    NSInteger weekday = [weekdayComponents weekday] - 1;
    NSLog(@"%d", weekday);
}

returns same numbers, but why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The behavior you see is correct. The weekday component is not affected by the firstWeekday property. If you have a date representing a sunday it will always be a sunday wether you start your week on that sunday or on monday. What this should affect is the week number in the week property of your date components.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...