一种更有效的毫秒转换方法?

时间:2010-06-01 01:36:20

标签: javascript

我目前正在使用此方法将毫秒转换为min:sec:1 / 10sec。 然而,它似乎根本没有效率。有谁知道更快更有效和优化的方法来实现同样的目标。

mills.prototype.formatTime = function(time) {   
    var elapsedTime = (time * 1000);
    //Minutes
    var elapsedM = (elapsedTime/60000)|0;
    var remaining = elapsedTime - (elapsedM * 60000);
    //add a leading zero if it's a single digit number
    if (elapsedM < 10) {
            elapsedM = "0"+elapsedM;
            }   
    //Seconds
    var elapsedS = ((remaining/1000)|0);
    remaining -= (elapsedS*1000);
        ////add leading zero
        if (elapsedS<10) {
            elapsedS = "0"+elapsedS;
        }
    //Hundredths
    var elapsedFractions = ((remaining/10)|0);  
        if (elapsedFractions < 10) {
            elapsedFractions = "0"+elapsedFractions;
        }
    //display results nicely
    var time_data = elapsedM+":"+elapsedS+":"+elapsedFractions;
    //return time_data;
    return[time_data,elapsedM,elapsedS,elapsedFractions]
    };

1 个答案:

答案 0 :(得分:4)

为什么不使用日期对象?

var dateObj=new Date(3500);
var minPart=dateObj.getMinutes();
var secondsPart=dateObj.getSeconds();
var tenthsPart=dateObj.getMilliseconds()/100;

alert(minPart+':'+secondsPart+':'+tenthsPart);

More info.