How to Get time difference in iPhone
-
07-07-2019 - |
Question
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 ?
Solution
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];
OTHER TIPS
NSDate
should work for you, use timeIntervalSinceDate:
, which returns a time interval which has millisecond precision.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow