Using moment.js how do I give a specific format for a duration

Viewed 1711

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 moment.js to convert this into formatted duration as follows:

 1. {d} {hh}:{mm}:{ss} (1d 00:01:01)
 2. {d}d {hh}h m{mm} s{ss} (1d 04h 30m 23s)

I would also like to ensure the following:

  • Days are trimmed if days is 0 (00:01:01 - for 1 minute 1 second)
  • hh:mm:ss are never trimmed, 1 second shows as 00:00:01.

I can create the duration like this:

moment.duration(duration_seconds, 'seconds')

The humanize function is not suitable as it approximates. I can also write my own with:

duration.get('d')
duration.get('h')
duration.get('m')
duration.get('s')

I can't seem to find a built in function but I assume this would be an obvious one? Is there something I am missing, otherwise I can PR one into moment library.

This seems to imply that the function still does not exist: Using moment.js, How can I simplify a duration to its most simplified form?

2 Answers
Related