1function convertHMS(value) {
2 const sec = parseInt(value, 10); // convert value to number if it's string
3 let hours = Math.floor(sec / 3600); // get hours
4 let minutes = Math.floor((sec - (hours * 3600)) / 60); // get minutes
5 let seconds = sec - (hours * 3600) - (minutes * 60); // get seconds
6 // add 0 if value < 10; Example: 2 => 02
7 if (hours < 10) {hours = "0"+hours;}
8 if (minutes < 10) {minutes = "0"+minutes;}
9 if (seconds < 10) {seconds = "0"+seconds;}
10 return hours+':'+minutes+':'+seconds; // Return is HH : MM : SS
11}
12
1// just added 2 formats in Glorious Guanaco code
2function secondsToHms(value) {
3 const sec = parseInt(value, 10);
4 let hours = Math.floor(sec / 3600);
5 let minutes = Math.floor((sec - hours * 3600) / 60);
6 let seconds = sec - hours * 3600 - minutes * 60;
7 if (hours < 10) { hours = '0' + hours; }
8 if (minutes < 10) { minutes = '0' + minutes; }
9 if (seconds < 10) { seconds = '0' + seconds; }
10 if (hours == 0) {
11 return +minutes + ':' + seconds; // Return in MM:SS format
12 } else {
13 return hours + ':' + minutes + ':' + seconds; // Return in HH:MM:SS format
14 }
15 }