2015-07-11 127 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())