how to find the difference between two dates in HH:MM:SS format in objective c?
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开发者_StackOverflow tried my luck but in vain.Can anybody help me out?
Thanks in advance.
Here's the steps that I think you'll need to perform:
Parse both the dates into
NSDate
objects using anNSDateFormatter
.Calculate the difference between the two dates using
NSTimeInterval ti = [laterDate timeIntervalSinceDate:earlierDate];
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:@"%lu:%02lu:%02lu", h, m, s];
Disclaimer
It is late at night for me so the above calculations for hours, minutes and seconds may not be correct.
精彩评论