在某些情况下,我需要在1天内增加一个NSDate.对于它,我使用dateByAddingTimeInterval,但它不起作用.
这是代码:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"]; Nsstring *startDate = [Nsstring stringWithFormat:@"%@/2012 %@",dayString,begin]; Nsstring *endStringForDate = [Nsstring stringWithFormat:@"%@/2012 %@",end]; NSLog(@"Csantos: event starts: %@,event ends: %@",startDate,endStringForDate); NSDate *beginDate = [dateFormat dateFromString:startDate]; NSDate *endDate = [dateFormat dateFromString:endStringForDate]; NSComparisonResult result = [beginDate compare:endDate]; if(result == NSOrderedDescending){ NSTimeInterval dayinseconds = 24 * 60 * 60; [endDate dateByAddingTimeInterval:dayinseconds]; NSLog(@"Csantos: event ends: %@",endDate); }
结果:
2012-01-24 12:09:47.837 app[3689:207] Csantos: event starts: 19/02/2012 23:00,event ends: 19/02/2012 03:00 2012-01-24 12:09:47.837 app[3689:207] Csantos: event ends: 19/02/2012 03:00
我已经尝试过addTimeInterval(不推荐使用,我知道),但它也没有用.怎么了?
问候!