Trax
Trax

Reputation: 1538

Moment.js time to format

I have this code :

var timeUntilUnlock = moment().to(moment.unix(result[0][0].locked))

The output is something like , in a hour, in a minute, in a few seconds. I was wondering if there is a way I can covert the output to display the actual number like. :

in 00:00:08

instead of saying in a few seconds.

Upvotes: 1

Views: 142

Answers (1)

Andy Gaskell
Andy Gaskell

Reputation: 31761

Use diff to get a duration:

var a = moment();
var b = moment().add(8, 'seconds');

var duration = moment.duration(a.diff(b));

You can use the methods on the duration object to get the values you want to display:

duration.hours() + ":" + duration.minutes() + ":" + duration.seconds()

If you want padded zeroes you'll have to do it yourself:

function pad(n) {
  n = Math.abs(n); // to handle negative durations
  return (n < 10) ? ("0" + n) : n;
}

pad(duration.hours()) + ":" + pad(duration.minutes()) + ":" + pad(duration.seconds())

Upvotes: 1

Related Questions