Nov*_*arg 1 objective-c nsdate nsdatecomponents ios
我知道这个问题被问了一百万次,我找不到解决办法.问题是:我正在修改一个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];
}
Run Code Online (Sandbox Code Playgroud)
这是调试器(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
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
谢谢.
日期以GMT显示:2012-02-29 00:00:00 +0000是2012-03-01 22:00:00 +0200.另外,请不要忘记夏令时变化,这解释了为什么有时会有3个小时的差异.
结果日期是正确的,但调试器将其显示在GMT中.
| 归档时间: |
|
| 查看次数: |
1252 次 |
| 最近记录: |