tags:

views:

29

answers:

1

I think the heading itself says what i want to do?

Stil,let me clarify. I am right now working on an application where i have two dated in a format like :

"YYYY:MM:DD HH:MM:SS"

Using which i need to calculate difference between the both dates in the format :

HH:MM:SS

I searched in the wiki and also tried my luck but in vain.Can anybody help me out?

Thanks in advance.

A: 

Here's the steps that I think you'll need to perform:

  1. Parse both the dates into NSDate objects using an NSDateFormatter.

  2. Calculate the difference between the two dates using

    NSTimeInterval ti = [laterDate timeIntervalSinceDate:earlierDate];
    
  3. The ti variable above now contains the number of seconds between the two dates. You can reformat that into hours, seconds and minutes with:

    NSUInteger h, m, s;
    h = (ti / 3600);
    m = ((NSUInteger)(ti / 60)) % 60;
    s = ((NSUInteger) ti) % 60;
    NSString *str = [NSString stringWithFormat:@"%d:%02d:%02d", h, m, s];
    

Disclaimer

It is late at night for me so the above calculations for hours, minutes and seconds may not be correct.

dreamlax