Search code examples
cocoatimensdatenstimeinterval

Calculate time difference in Cocoa


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

Solution

  • 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