Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use format() on a moment.js duration?

People also ask

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().


// set up
let start = moment("2018-05-16 12:00:00"); // some random moment in time (in ms)
let end = moment("2018-05-16 12:22:00"); // some random moment after start (in ms)
let diff = end.diff(start);

// execution
let f = moment.utc(diff).format("HH:mm:ss.SSS");
alert(f);

Have a look at the JSFiddle


convert duration to ms and then to moment:

moment.utc(duration.as('milliseconds')).format('HH:mm:ss')

We are looking into adding some kind of formatting to durations in moment.js. See https://github.com/timrwood/moment/issues/463

A couple other libraries that might help out are http://countdownjs.org/ and https://github.com/icambron/twix.js


Use this plugin Moment Duration Format.

Example:

moment.duration(123, "minutes").format("h:mm");

Use this line of code:

moment.utc(moment.duration(4500, "seconds").asMilliseconds()).format("HH:mm:ss")

var diff = moment(end).unix() - moment(start).unix();
moment.utc(moment.duration(diff).asMilliseconds()).format("HH:mm:ss.SSS");