Calculate time difference in Cocoa

Luuk Beursgens picture Luuk Beursgens · Mar 17, 2009 · Viewed 22.8k times · Source

I have got two timevalues in the format: %H%M%S (E.G.161500)

These values are text-based integers.

Is there a simple function in Cocoa that calculates the difference between these two integers using a 60 seconds and minutes scale?

So if

time 1 = 161500
time 2 = 171500

timedifference = 003000

Answer

Can Berk Güder picture Can Berk Güder · Mar 17, 2009
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HHmmss"];

NSDate *date1 = [dateFormatter dateFromString:@"161500"];
NSDate *date2 = [dateFormatter dateFromString:@"171500"];

NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // diff = 3600.0