比较两个日期(currentDate
和_timerStartDate
)后,我得到以下输出
nstimeinterval: 88.998212
2014-05-14 14:07:08.284 currentDate: 14-05-2014 14:07:08
2014-05-14 14:07:08.284 stopDate: 14-05-2014 14:05:39
2014-05-14 14:07:08.284 hour: 10
这是正确和预期的。然而,当我打破timerDate
时,我从NSLog获得了10个小时......我期待零。我做错了什么?
这是我的代码:
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:_timerStartDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
NSLog(@"nstimeinterval: %f", timeInterval);
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSLog(@"currentDate: %@", [dateFormatter stringFromDate:currentDate]);
NSLog(@"stopDate: %@", [dateFormatter stringFromDate:_timerStartDate]);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: ( NSHourCalendarUnit | NSYearCalendarUnit) fromDate:timerDate];
NSLog(@"year: %li", (long)components.year);
NSLog(@"timer: %@", timeString);
timerTicksForCounter = timeString;
NSLog(@"hour: %li", (long)components.hour);
答案 0 :(得分:0)
你真正想要完成的是什么?使用两个日期之间的时间填充NSDateComponents
?在这种情况下,您可能需要-[NSCalendar components:fromDate:toDate:options:]
,其中起始日期为开始日期,最新日期为您停止时的当前日期。与此同时,您似乎在进行大量复杂且可能不必要的日期混搭。