views:

1105

answers:

2

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.

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

Thanks for the help.

A: 

NSDate should work for you, use timeIntervalSinceDate:date, which returns a time interval which has millisecond precision, heres a reference http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDate%5FClass/Reference/Reference.html#//apple%5Fref/occ/instm/NSDate/timeIntervalSinceDate:

Daniel
+7  A: 

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];
Nathan de Vries
NSTimeInterval interval = [secondDate timeIntervalSinceDate:firstDate];
Jon Hess