NSCalendar,为什么设置firstWeekday不影响计算结果? [英] 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?
推荐答案
您看到的行为是正确的。 weekday组件不受firstWeekday属性的影响。如果你有一个日期代表星期天,它将永远是一个星期天潮湿你开始你的星期星期天或星期一。这应该影响是您的日期组件的周属性中的周数。
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.
这篇关于NSCalendar,为什么设置firstWeekday不影响计算结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!