NSDateComponents weekOfYear返回错误的值

NSDateComponents weekOfYear返回错误的值

问题描述:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
[calendar setTimeZone:[NSTimeZone defaultTimeZone]];
[calendar setFirstWeekday:2];
[calendar setMinimumDaysInFirstWeek:1];

NSDateComponents *mockTodayComponents = [[NSDateComponents alloc] init];
mockTodayComponents.day = 28;
mockTodayComponents.month = 12;
mockTodayComponents.year = 2015;
NSDate *date = [calendar dateFromComponents:mockTodayComponents];
NSDateComponents *c = [calendar components:NSCalendarUnitWeekOfYear | NSCalendarUnitYearForWeekOfYear fromDate:date];

NSInteger week = [c weekOfYear];

week返回1而不是53,因此第二年的枚举错误也是因为它,我的代码出了什么问题或者是Apple的错误?

week returns 1 instead of 53 and ofcoaurse the next year weeks enumeration wrong also because of it, what's wrong with my code or it's Apple's bug?

您的minimumDaysInFirstWeek为1.这意味着在新年中至少有1天的任何一周都是该年的第一周. 文件

Your minimumDaysInFirstWeek is 1. That means that any week that has at least 1 day in the new year is the 1st week of that year. Docs

根据 ISO :

第01周的描述彼此等效:

There are mutually equivalent descriptions of week 01:

其中有一年中第一个星期四的那一周(正式的ISO 定义),其中包含1月4日的一周,包含 起始年份和一周中大部分(四天或以上)的工作日 从12月29日至1月4日的星期一开始.

the week with the year's first Thursday in it (the formal ISO definition), the week with 4 January in it, the first week with the majority (four or more) of its days in the starting year, and the week starting with the Monday in the period 29 December – 4 January.