Format seconds as minutes:seconds (Source)
function formatSecondsMS(s) {
return (s - (s %= 60)) / 60 + (9 < s ? ':' : ':0') + s
}
formatSecondsMS('599') returns 9:59
Format seconds as hours:minutes:seconds (Source)
function formatSecondsHMS(seconds) {
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.round(seconds % 60);
return [
h,
m > 9 ? m : (h ? '0' + m : m || '0'),
s > 9 ? s : '0' + s
].filter(Boolean).join(':');
}
formatSecondsHMS('3699') returns 1:01:39