Given a duration of a number of seconds coming from an API as duration_seconds = 86485
.
(1 day, 0 hours, 1 minute, 1 second)
I was going to use
The moment-duration-format plugin can assist you with this.
// your input
var duration_seconds = 86485;
// create a moment-duration object
var duration = moment.duration(duration_seconds, 'seconds');
// format the object with a string
var formatted = duration.format('h[h] m[m] s[s]');
// displaying the output here
document.getElementById('output').innerHTML = formatted;
Note that by default it will omit units that are not relevant, so if your input is 3, it's just going to say "3s"
, not "0h 0m 3s"
.
If you want to change this behavior, set trim:false
, per the documentation. For example, to get the format you mentioned in comments, use:
.format('hh:mm:ss', { trim: false })