Moment.js 格式化时间

Moment.js time to format

我有这个代码:

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

输出类似于,一小时后,一分钟后,几秒钟后。我想知道是否有一种方法可以隐藏输出以显示实际数字。 :

in 00:00:08

而不是几秒钟后说。

使用diff to get a duration:

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

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

您可以使用持续时间对象上的方法来获取要显示的值:

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

如果你想要填充零,你必须自己做:

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())