2011-04-07 500 views

回答

32
var ms = 125230.41294642858, 
    min = 0|(ms/1000/60), 
    sec = 0|(ms/1000) % 60; 

alert(min + ':' + sec); 
+0

太棒了!謝謝:) – PeeHaa 2011-04-07 22:47:04

+1

'<<'操作符在做什麼? – JaredPar 2011-04-07 22:49:51

+3

@JaredPar - 'Math.floor'通過一個隱式的'int'投射:) – 2011-04-07 22:50:31

2

請嘗試以下

var num = Number(theTextValue); 
var seconds = Math.floor(num/1000); 
var minutes = Math.floor(seconds/60); 
var seconds = seconds - (minutes * 60); 
var format = minutes + ':' + seconds 
2
Number.prototype.toTime = function(){ 
    var self = this/1000; 
    var min = (self) << 0; 
    var sec = (self*60) % 60; 
    if (sec == 0) sec = '00'; 

    return min + ':' + sec 
}; 


var ms = (new Number('250')).toTime(); 
console.log(ms); 
=> '0:15' 

var ms = (new Number('10500')).toTime(); 
console.log(ms); 
=> '10:30' 
2

即使moment.js不提供這樣的功能,如果你來到這裏,你已經在使用moment.js,試試這個:

function getFormattedMs(ms) { 
    var duration = moment.duration(ms); 
    return moment.utc(duration.asMilliseconds()).format("mm:ss"); 
} 

這個解決方法是在Issue中引入的。

相關問題