NSDateComponents weekOfYear返回错误的值 [英] NSDateComponents weekOfYear returns wrong value

查看:190
本文介绍了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.

这篇关于NSDateComponents weekOfYear返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆