Iphone NSDate月加减法
我有一个类,它包含一个开始日期和一个结束日期,通常初始化为每月的第一秒和最后一秒 以下函数从2010年11月向前到12月再向后正常工作,但是从11月向后到startDate设置为 2010-09-30 23:00:00GMT 一个月零一小时前 奇怪的是,endDate仍然正确地设置为 2010-11-01 00:00:00格林威治标准时间 从这个不正确的日期向前一个月也会得到正确的时间和日期 这是一只虫子还是我在做不该做的事 -(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]; } -(void)moveMonth:(NSInteger)按金额{//正或负的月数 NSCalendar*cal=[NSCalendar当前日历]; NSDateComponents*components=[[[NSDateComponents alloc]init]autorelease]; //更新开始日期 [组件设置月:按金额]; NSDate*newStartDate=[cal dateByAddingComponents:components toDate:[self startDate]选项:0]; [自设置开始日期:newStartDate]; //结束日期呢 [月份:1]; NSDate*newEndDate=[cal dateByAddingComponents:components toDate:[self startDate]选项:0]; [self-setEndDate:newEndDate]; } 解决方案:回答正确指出这是DST问题 如果您希望以绝对时间和日期进行交易,则使用以下选项可避免涉及任何DST NSCalendar *cal = [[NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"GMT"]; [cal setTimeZone:zone]; NSCalendar*cal=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]自动释放]; NSTimeZone*zone=[NSTimeZone timeZoneWithName:@“GMT”]; [校准设置时区:时区];Iphone NSDate月加减法,iphone,objective-c,ios,Iphone,Objective C,Ios,我有一个类,它包含一个开始日期和一个结束日期,通常初始化为每月的第一秒和最后一秒 以下函数从2010年11月向前到12月再向后正常工作,但是从11月向后到startDate设置为 2010-09-30 23:00:00GMT 一个月零一小时前 奇怪的是,endDate仍然正确地设置为 2010-11-01 00:00:00格林威治标准时间 从这个不正确的日期向前一个月也会得到正确的时间和日期 这是一只虫子还是我在做不该做的事 -(void) moveMonth:(NSInteger)byAmou
这可能不是一个bug,而是与10月至11月期间的DST更改有关。只需获取当前日期的月份和年份,加上/减去月数差,然后根据这些新值生成日期就更容易了。无需担心日光节约的变化、闰年等。类似的事情应该会起作用:
-(void) moveMonth:(NSInteger)byAmount {
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
// we're just interested in the month and year components
NSDateComponents *nowComps = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit)
fromDate:now];
NSInteger month = [nowComps month];
NSInteger year = [nowComps year];
// now calculate the new month and year values
NSInteger newMonth = month + byAmount;
// deal with overflow/underflow
NSInteger newYear = year + newMonth / 12;
newMonth = newMonth % 12;
// month is 1-based, so if we've ended up with the 0th month,
// make it the 12th month of the previous year
if (newMonth == 0) {
newMonth = 12;
newYear = newYear - 1;
}
NSDateComponents *newStartDateComps = [[NSDateComponents alloc] init];
[newStartDateComps setYear: year];
[newStartDateComps setMonth: month];
[self setStartDate:[cal dateFromComponents:newDateComps]];
[newDateComps release];
// Calculate newEndDate in a similar fashion, calling setMinutes:59,
// setHour:23, setSeconds:59 on the NSDateComponents object if you
// want the last second of the day
}
这里有一个正确的方法。此方法在添加/减去月份“byAmount”后返回新的NSDate
那正是我忘记的事,我太傻了。
-(NSDate*) moveMonth:(NSInteger)byAmount {
NSDate *now = [NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMonth:byAmount];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:now options:0];
return newDate;
}