var measuredTime = new Date(null); measuredTime.setSeconds(4995); // specify value of SECONDS var MHSTime = measuredTime.toISOString().substr(11, 8);
So you can see that it is a very simple and easy way to convert seconds into HH-MM-SS formatted time. Here in this example code, I have specified the seconds as 4995 and it returns the 1 as the hour, 23 as minutes and 15 as seconds. It returns the output 01:23:15.
You can also try the above code and test it. You will get the well formatted time from seconds.
Alternative way of converting seconds in HH-MM-SS format
You can also convert seconds using mathematical operations.
Below is another example code of doing the same task with the mathematical operation:
hours = Math.floor(totalSeconds / 3600); totalSeconds %= 3600; minutes = Math.floor(totalSeconds / 60); seconds = totalSeconds % 60;