Last Updated: August 21, 2017
·
91.63K
· ashiguruma

Converting Milliseconds to HH:MM:SS.mmm

Useful code for converting a duration in milliseconds to a human readable format.

function msToTime(duration) {
    var milliseconds = parseInt((duration%1000)/100)
        , seconds = parseInt((duration/1000)%60)
        , minutes = parseInt((duration/(1000*60))%60)
        , hours = parseInt((duration/(1000*60*60))%24);

    hours = (hours < 10) ? "0" + hours : hours;
    minutes = (minutes < 10) ? "0" + minutes : minutes;
    seconds = (seconds < 10) ? "0" + seconds : seconds;

    return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}

Created from http://stackoverflow.com/questions/9763441/milliseconds-to-time-in-javascript

1 Response
Add your response

milliseconds shouldn't be divided by 100 in the second line

over 1 year ago ·