与我预期的相反,从 NSDateComponents 对象设置 weekOfMonth 属性对 weekOfYear 没有影响......
NSDateComponents *iterativeComponents = [calendar components: NSYearCalendarUnit|
NSMonthCalendarUnit|
NSWeekOfYearCalendarUnit|
NSWeekOfMonthCalendarUnit|
NSWeekdayCalendarUnit
fromDate:referenceDate];
for (int j = 4; j > -1; j--) {
iterativeComponents.weekOfMonth = j;
NSDate *dateForWeek = [calendar dateFromComponents:iterativeComponents];
}
设置 iterativeComponents 的 weekOfMonth 不会改变 weekOfYear 并且给我留下一个不一致的 NSDateComponents 对象:
<NSDateComponents: 0x9aac9e0>
Calendar Year: 2012
Month: 9
Leap month: no
Week of Year: 36
Week of Month: 3
Weekday: 2
快速查看日历告诉我,2009 年 9 月的第三周是一年中的第 37 周。iterativeComponents 生成的日期是
2012-09-02 22:00:00 +0000
如何通过设置 weekOfMonth 来正确计算 weekOfYear 属性?