Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seconds to HHMMSS (Greater Than 24 Hours)

Tags:

javascript

I have a sql query that returns customer wait time in seconds. The following javascript is used to convert seconds to HH:MM:SS, but does not work when the sum of wait time is greater than 24 hours. For example, if wait time is 600 seconds, it displays correctly as 00:10:00. However, if the wait time is 90600 seconds, it displays as 01:10:00 rather than 25:10:00. Any help is greatly appreciated.

if (format == 'secondsToHHMMSS') {
var dt = new Date();
var dtToday = new Date();
var dt = new Date(dtToday.getFullYear(), dtToday.getMonth(), dtToday.getDate(),0,0,0);
dt.setSeconds(num);
return add0(dt.getHours()) + ':' + add0(dt.getMinutes()) + ':' +  add0(dt.getSeconds());
}
like image 338
Justin Hill Avatar asked Oct 16 '25 11:10

Justin Hill


1 Answers

function secondsToHHMMSS (seconds) {
    return (Math.floor(seconds / 3600)) + ":" + ("0" + Math.floor(seconds / 60) % 60).slice(-2) + ":" + ("0" + seconds % 60).slice(-2)
}
    
console.log(secondsToHHMMSS(600), secondsToHHMMSS(90600))    
like image 158
juvian Avatar answered Oct 18 '25 23:10

juvian