user379760
user379760

Reputation: 158

how to calculate the time in the hr:min:sec

Can some one help me out from the time calculation in form of hr:min:sec. But I does need the system time. That's for I made this code

I try to make time calculation in the form of hr:min:sec but it doesn't work in right way(it work till one hours)& ((1)after the 60 second output is 0:1:60 (2)after the 61 second output is 0:1:71 (3)after after the 3600 second output is 1:1:00 but(3)after the 3601 second output is wrong )

Here is the code:

In the .m file

-(IBAction)start
{      
    timer1 = [NSTimer scheduledTimerWithTimeInterval:(.01) target:self selector:@selector(timepassed) userInfo:nil repeats:YES];        
}

-(void)timepassed
{
     counter++;
    if(sec==60)
        sec=0;
    else 
        sec=counter/100;

    if(min==60)
        min=0;
    else 
        min=sec/60;

    if(hr==24)
        hr=0;
    else 
        hr=min/60;

    m1++;
    NSString *l=[NSString stringWithFormat:@"%i:%i:%i",hr,min,sec];

    [m setText:l];
}

Upvotes: 0

Views: 248

Answers (1)

Eiko
Eiko

Reputation: 25632

-(void) timepassed
{
    counter++;
    if (counter == 60)  { secs++; counter = 0; }
    if (secs == 60) { min++; secs = 0; }
    if (min == 60) { hr++; min = 0; }

    // Your usual output here
}

Keep in mind that this sets the counter variable different than your code, i.e. it stores only 1/100 secs from 0...99.

There are of cause a dozen of algorithms to translate times into hrs:mins:secs, this is just one of them that's similar to your code.

Side note: Please accept answers that did help you (also on your older questions), this is common behavior on this site and keeps people motivated to help you.

Upvotes: 2

Related Questions