Dea*_*ith 2 iphone objective-c ios
我有一个班级,其中包含开始日期和结束日期,通常初始化为本月的最后一个月和最后一个月.
以下功能可以正常工作,从2010年11月开始到12月再返回,但是从11月开始倒退,最后将startDate设置为
2010-09-30 23:00:00 GMT
IE浏览器.一个月一小时前.
奇怪的是,endDate仍然正确设置为
2010-11-01 00:00:00 GMT
从这个不正确的日期前进一个月也会产生正确的时间和日期.
这是一个错误还是我在做一些我不应该做的事情?
-(void) moveMonth:(NSInteger)byAmount { // Positive or negative number of months
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
// Update the start date
[components setMonth:byAmount];
NSDate *newStartDate = [cal dateByAddingComponents:components toDate:[self startDate] options:0];
[self setStartDate:newStartDate];
// And the end date
[components setMonth:1];
NSDate *newEndDate = [cal dateByAddingComponents:components toDate:[self startDate] options:0 ];
[self setEndDate:newEndDate];
}
解决方案:正确回答指出这是DST问题
如果您想要处理绝对时间和日期,那么使用以下内容可以避免涉及任何DST.
NSCalendar *cal = [[NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"GMT"];
[cal setTimeZone:zone];
| 归档时间: |
|
| 查看次数: |
3238 次 |
| 最近记录: |