A more effecient millisecond conversion method?

Posted by cube on Stack Overflow See other posts from Stack Overflow or by cube
Published on 2010-06-01T01:36:20Z Indexed on 2010/06/01 1:43 UTC
Read the original article Hit count: 240

Filed under:

I am currently using this method to convert milliseconds to min:sec:1/10sec. However it does not seem to be efficient at all. Would anyone know of a faster more efficient and optimized way of accomplishing the same.

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]
    };

© Stack Overflow or respective owner

Related posts about JavaScript