我可以使用这种方法获得两个日期之间的时间间隔:
- (NSString *)calculateTime:(NSDate *)fromDateTime andDate:(NSDate *)toDateTime {
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [calendar components:unitFlags fromDate:fromDateTime toDate:toDateTime options:0];
int hours = [conversionInfo hour];
int minutes = [conversionInfo minute];
int seconds = [conversionInfo second];
return [NSString stringWithFormat:@"%02dh %02dm %02ds", hours, minutes, seconds];
}
假设我有 3 个NSDate
范围:
2013-02-03 11:20:43 - 2013-02-03 12:42:12 // 01h 21m 29s
2013-02-04 22:11:29 - 2013-02-05 01:56:30 // 03h 45m 01s
2013-02-05 14:02:10 - 2013-02-05 18:32:08 // 04h 29m 58s
如何%02dh %02dm %02ds
为所有范围的间隔实现相同的格式NSDate
?换句话说,计算所有区间的总和。
任何建议,将不胜感激。