Last Updated: August 21, 2017
·
94.47K
· 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 ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside