wkdefg
Last Updated: August 21, 2017
·
36.58K
· ashiguruma
C6b33788d5d2de4533488f8707026748

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

Say Thanks
Respond

1 Response
Add your response

29233

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

6 months ago ·