This solution uses one function
to split milliseconds into a parts object
, and another function
to format the parts object
.
I created 2 format functions, one as you requested, and another that prints a friendly string and considering singular/plural, and includes an option to show milliseconds.
function parseDuration(duration) {_x000D_
let remain = duration_x000D_
_x000D_
let days = Math.floor(remain / (1000 * 60 * 60 * 24))_x000D_
remain = remain % (1000 * 60 * 60 * 24)_x000D_
_x000D_
let hours = Math.floor(remain / (1000 * 60 * 60))_x000D_
remain = remain % (1000 * 60 * 60)_x000D_
_x000D_
let minutes = Math.floor(remain / (1000 * 60))_x000D_
remain = remain % (1000 * 60)_x000D_
_x000D_
let seconds = Math.floor(remain / (1000))_x000D_
remain = remain % (1000)_x000D_
_x000D_
let milliseconds = remain_x000D_
_x000D_
return {_x000D_
days,_x000D_
hours,_x000D_
minutes,_x000D_
seconds,_x000D_
milliseconds_x000D_
};_x000D_
}_x000D_
_x000D_
function formatTime(o, useMilli = false) {_x000D_
let parts = []_x000D_
if (o.days) {_x000D_
let ret = o.days + ' day'_x000D_
if (o.days !== 1) {_x000D_
ret += 's'_x000D_
}_x000D_
parts.push(ret)_x000D_
}_x000D_
if (o.hours) {_x000D_
let ret = o.hours + ' hour'_x000D_
if (o.hours !== 1) {_x000D_
ret += 's'_x000D_
}_x000D_
parts.push(ret)_x000D_
}_x000D_
if (o.minutes) {_x000D_
let ret = o.minutes + ' minute'_x000D_
if (o.minutes !== 1) {_x000D_
ret += 's'_x000D_
}_x000D_
parts.push(ret)_x000D_
_x000D_
}_x000D_
if (o.seconds) {_x000D_
let ret = o.seconds + ' second'_x000D_
if (o.seconds !== 1) {_x000D_
ret += 's'_x000D_
}_x000D_
parts.push(ret)_x000D_
}_x000D_
if (useMilli && o.milliseconds) {_x000D_
let ret = o.milliseconds + ' millisecond'_x000D_
if (o.milliseconds !== 1) {_x000D_
ret += 's'_x000D_
}_x000D_
parts.push(ret)_x000D_
}_x000D_
if (parts.length === 0) {_x000D_
return 'instantly'_x000D_
} else {_x000D_
return parts.join(' ')_x000D_
}_x000D_
}_x000D_
_x000D_
function formatTimeHMS(o) {_x000D_
let hours = o.hours.toString()_x000D_
if (hours.length === 1) hours = '0' + hours_x000D_
_x000D_
let minutes = o.minutes.toString()_x000D_
if (minutes.length === 1) minutes = '0' + minutes_x000D_
_x000D_
let seconds = o.seconds.toString()_x000D_
if (seconds.length === 1) seconds = '0' + seconds_x000D_
_x000D_
return hours + ":" + minutes + ":" + seconds_x000D_
}_x000D_
_x000D_
function formatDurationHMS(duration) {_x000D_
let time = parseDuration(duration)_x000D_
return formatTimeHMS(time)_x000D_
}_x000D_
_x000D_
function formatDuration(duration, useMilli = false) {_x000D_
let time = parseDuration(duration)_x000D_
return formatTime(time, useMilli)_x000D_
}_x000D_
_x000D_
_x000D_
console.log(formatDurationHMS(57742343234))_x000D_
_x000D_
console.log(formatDuration(57742343234))_x000D_
console.log(formatDuration(5423401000))_x000D_
console.log(formatDuration(500))_x000D_
console.log(formatDuration(500, true))_x000D_
console.log(formatDuration(1000 * 30))_x000D_
console.log(formatDuration(1000 * 60 * 30))_x000D_
console.log(formatDuration(1000 * 60 * 60 * 12))_x000D_
console.log(formatDuration(1000 * 60 * 60 * 1))
_x000D_