6

I've already tried with NSDate but with no luck. I want the difference between for example 14:10 and 18:30.

Hours and minutes.

I Hope you can help me shouldn't be that complicated :)

bdukes
  • 152,002
  • 23
  • 148
  • 175
user134282
  • 153
  • 2
  • 3
  • 13

4 Answers4

30

There's no need to calculate this by hand, take a look at NSCalendar. If you want to get the hours and minutes between two dates, use something like this:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                    fromDate:firstDate
                                                      toDate:otherDate
                                                     options:0];
[gregorianCalendar release];

You now have the hours and minutes as NSDateComponents and can access them as NSIntegers like [components hour] and [components minute]. This will also work for hours between days, leap years and other fun stuff.

Pascal
  • 16,846
  • 4
  • 60
  • 69
19

Here's my quick solution:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"HH:mm"];
NSDate *date1 = [df dateFromString:@"14:10"];
NSDate *date2 = [df dateFromString:@"18:09"];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
int hours = (int)interval / 3600;             // integer division to get the hours part
int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
NSString *timeDiff = [NSString stringWithFormat:@"%d:%02d", hours, minutes];
amrox
  • 6,207
  • 3
  • 36
  • 57
12

The NSDate class has a method timeIntervalSinceDate that does the trick.

NSTimeInterval secondsBetween = [firstDate timeIntervalSinceDate:secondDate];

NSTimeInterval is a double that represents the seconds between the two times.

NWCoder
  • 5,296
  • 1
  • 26
  • 23
  • You then have to do your own calculations to turn the seconds into hours and minutes. Then you have to convert those numbers into a string. It's trivial but it comes up a lot. I suggest creating utility class to do this and keep it handy. – TechZen Nov 24 '09 at 20:06
  • okay thanks! now i have the seconds so to get hours i would divide through 3600. then i get 1546521531. Can you give me any hints please? – user134282 Nov 24 '09 at 20:32
7
NSString *duration = [self calculateDuration:oldTime secondDate:currentTime];

- (NSString *)calculateDuration:(NSDate *)oldTime secondDate:(NSDate *)currentTime
{
    NSDate *date1 = oldTime;
    NSDate *date2 = currentTime;

    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

    int hh = secondsBetween / (60*60);
    double rem = fmod(secondsBetween, (60*60));
    int mm = rem / 60;
    rem = fmod(rem, 60);
    int ss = rem;

    NSString *str = [NSString stringWithFormat:@"%02d:%02d:%02d",hh,mm,ss];

    return str;
}
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89