【发布时间】:2011-06-22 16:06:42
【问题描述】:
因此,当使用 NSCalendar 和 NSDateComponents 从 NSDate 对象中提取日期组件时,我遇到了一个奇怪的行为。 如果日期距离 1970 年 0 秒,则周组件将返回 53。 除了模数 52 的明显方法之外,是否有对此的解释或解决方法?
这是您可以在您的机器上运行以进行测试的代码:
-
(void)testDate {
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDateComponents *comp = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekCalendarUnit) fromDate:date];
DLog(@"%d/%d/%d week: %d", [comp day],[comp month], [comp year], [comp week]);
}
这是输出:
31/12/1969 week: 53
(gdb) po date
1970-01-01 00:00:00 +0000
(gdb)
【问题讨论】:
标签: objective-c nsdate nscalendar nsdatecomponent