我需要計算工作日給定日期,但是,根據在日曆上一個星期能星在週一和somwhere週日NSCalendar,爲什麼設置第一個週日不影響計算結果?
所以我想設置它,開始於週一,使用
[[NSCalendar currentCalendar] setFirstWeekday:2];
然而,計算的結果是一樣的
{
[[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);
}
返回相同的數字,但是爲什麼呢?
所以週日永遠是週日0,無論第一個週日點是什麼? –
週日永遠是1 – Sven
週日1,週一2,週二3,週三4,週五5,週六6 –