views:

78

answers:

1

I am using an NSTimer which I have working to show minutes and seconds. But I am confused about the math needed to calculate hours.

I am using:

- (void)updateCounter:(NSTimer *)theTimer {
    static int count = 0;
    count += 1;
    int seconds = count % 60;
    int minutes = (count - seconds) / 60;
    // Not sure how to calculate hours
    int hours = (count - minutes) / 60;
    self.timer.text = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
}

What calculation should I use for hours?

+1  A: 
seconds = ...
seconds_in_minute = seconds % 60;
minutes_in_hour = ( seconds / 60 ) % 60;
hour_in_day = ( seconds / 3600 ) % 24;
drawnonward