Search code examples
iphoneios5nstimeinterval

Converstion of seconds in minutes


I have to change seconds into minutes, hours. I am already using this code:

   int minutes = floor(seconds/60);
   int sec = trunc(seconds - minutes * 60);
   int hours1 = floor(seconds / (60 * 60));

It works fine, but when "60 minutes" completed it gives 01:61:01 I want timer in this format 01:01:01. i.e. my minutes does not convert after 60 minutes.

Please suggest me or help me. I am searching lots of code but still not find exact solution.


Solution

  • you can calculate minutes from seconds with bellow formula...

    int minutes = (secondsLeft % 3600) / 60;
    

    see the whole code for hours,minutes and seconds bellow...

    int hours, minutes, seconds;
    hours = secondsLeft / 3600;
    minutes = (secondsLeft % 3600) / 60;
    seconds = (secondsLeft %3600) % 60;
    NSLog(@"\nTime is => %02d:%02d:%02d",hours,minutes,seconds);
    

    Output is : Time is 00:00:06