【问题标题】:NSDateComponents weekOfYear returns wrong valueNSDateComponents weekOfYear 返回错误值
【发布时间】:2015-12-13 09:15:26
【问题描述】:
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 的错误?

【问题讨论】:

    标签: objective-c nsdate nscalendar nsdatecomponents


    【解决方案1】:

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

    根据ISO:

    第 1 周有相互等价的描述:

    包含一年中第一个星期四的那一周(正式的 ISO 定义),包含 1 月 4 日的那一周,包含 1 月 4 日的第一周 开始年的大多数(四天或更多)天,以及一周 从 12 月 29 日至 1 月 4 日期间的星期一开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多