Seconds to Hours, Minutes and Seconds (HH:MM:SS)

Shape Image One

[fenomen_title title=”Program”]

C/C++ Prpgram

void main(){

  int seconds=4545;

  //convret seconds into hr, min, sec
  int hr=(int)(seconds/3600);
  int min=((int)(seconds/60))%60;
  int sec=(int)(seconds%60);

  printf("%d:%d:%d",hr, min, sec);
}

Output:

1:15:45

Java Program

class Simple{  
  public static void main(String args[]){  
       
    int seconds=4545;

    //convret seconds into hr, min, sec
    int hr=(int)(seconds/3600);
    int min=((int)(seconds/60))%60;
    int sec=(int)(seconds%60);

    System.out.println(hr+":"+min+":"+sec);
  }
}

Output

1:15:45

Python Program


seconds=4545

#convert seconds to hours, minutes and seconds
hr=int(seconds/3600)
min=int(seconds/60)%60
sec=int(seconds%60)

#formate time in hh:mm:ss format
hhmmss="%s:%s:%s"%(str(hr).zfill(2), str(min).zfill(2), str(sec).zfill(2))

print(hhmmss)

Output:

01:15:45

JavaScript Program


(function(){

seconds=4545;

/*convert seconds into hours, minutes and seconds*/
hr=Math.floor(seconds/3600);
min=Math.floor(seconds/60)%60;
sec=seconds%60;

/*padding leading zero if the number is single digit*/
hr=(hr< 10 ? '0' : '') + hr;
min=(min< 10 ? '0' : '') + min;
sec=(sec< 10 ? '0' : '') + sec;

return hr+':'+min+':'+sec;
})();

Output:

01:15:45