7

我有 2 个包含时间值的数组。它们采用以下格式。mm:ss:数百秒。我想得到数组中两个 [lastObjects] 之间的差异。NSDate 不起作用,因为最后一个值以百分之一秒为单位。

一个问题。如果第二个日期大于第一个日期,它会给我一个负数,例如 -01:10:00 吗?

4

2 回答 2

36

您的问题有两个部分,解析时间并获得差异:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"mm:ss:SS"];

NSDate* firstDate = [dateFormatter dateFromString:@"01:00:00"];
NSDate* secondDate = [dateFormatter dateFromString:@"01:02:00"];

NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
于 2009-09-15T14:10:31.760 回答
1

NSDate应该为你工作, use timeIntervalSinceDate:,它返回一个具有毫秒精度的时间间隔。

于 2009-09-15T13:34:25.663 回答