我知道这个问题被问了一百万次,我就是找不到解决方案。问题是:我正在修改NSDate
以返回当月第一天的日期或下个月第一天的日期。除了一件事之外,它似乎工作正常:它使时间减少了 2 或 3 小时。
代码:
NSCalendar *theCalendar = [NSCalendar currentCalendar];
int comp = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *theComp = [theCalendar components:comp fromDate:date];
[theComp setTimeZone:[NSTimeZone systemTimeZone]];
switch (roundingMode) {
case FIRST_OF_MONTH:
[theComp setDay:1];
return [theCalendar dateFromComponents:theComp];
case FIRST_OF_NEXT_MONTH:
[theComp setMonth:theComp.month+1];
[theComp setDay:1];
return [theCalendar dateFromComponents:theComp];
}
这是调试器(gdb)的输出:
(gdb) po theComp
<NSDateComponents: 0x6e26170>
TimeZone: Europe/Kiev (EET) offset 7200
Calendar Year: 2012
Month: 3
Day: 1
(gdb) po date
2012-03-12 13:05:22 +0000
(gdb) po [theCalendar dateFromComponents:theComp]
2012-02-29 22:00:00 +0000
任何帮助将不胜感激。
谢谢你。