如何在iPhone中获得时差

问题描述:

我有2个带有时间值的数组。它们采用以下格式。 mm:ss:几百秒。我想得到数组中两个[lastObjects]之间的区别。 NSDate无法正常工作,因为最后一个值是百分之一秒。

I have 2 arrays with time values in it. They are in the following format. mm:ss:hundreds of a sec. I want to get the difference between the two [lastObjects] in the arrays. NSDate is not working because the last value is in hundredsth of a sec.

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

A question. If the second date is larger than the first will it give me a negative number like -01:10:00 ?

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

Your problem has two parts, parsing the time and getting the difference:

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];