7

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 ?

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
New to iPhone
  • 215
  • 2
  • 4
  • 11
  • You are not supposed to copy answers into your question. If an answer needs clarification, you comment on the answer and the author of the answer will respond. The solution I've given you should be enough to point you in the right direction. The documentation for NSDateFormatter will tell you everything you need to know about parsing times, and the documentation for NSDate will tell you everything you need to know about finding the difference between times, including the behavior of identical dates and negative time intervals. – Nathan de Vries Sep 16 '09 at 03:57
  • Whoops, it was more than enough. I apologize for not following protocol. – New to iPhone Sep 16 '09 at 18:56

2 Answers2

36

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
  • 15,481
  • 4
  • 49
  • 55
1

NSDate should work for you, use timeIntervalSinceDate:, which returns a time interval which has millisecond precision.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
Daniel
  • 22,363
  • 9
  • 64
  • 71