如何在iPhone中获得时差

New*_*one 7 iphone objective-c nsdate nstimeinterval

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

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

Nat*_*ies 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];
Run Code Online (Sandbox Code Playgroud)

  • NSTimeInterval interval = [secondDate timeIntervalSinceDate:firstDate]; (2认同)