2015-07-11 86 views
1

我有这样的代码:Moment.js时间格式化

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

的输出是一样的东西,在一个小时内,在一分钟内,在几秒钟。我想知道是否有一种方法可以隐藏输出来显示实际的数字。 :

in 00:00:08 

而不是在几秒钟内说。

回答

1

使用diff获得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())