stefan22
11/18/2017 - 5:15 PM

convert milliseconds to HH:MM:SS.mmm with javascript function or built-in method

convert milliseconds to HH:MM:SS.mmm with javascript function or built-in method

//convert date to millisecs
//using getTime();  then

new Date(1511026200000).toUTCString();
//returns => "Sat, 18 Nov 2017 17:30:00 GMT"

//or

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;
}
//call fn
msToTime(1511026200000);
//returns
//=> "17:30:00.0"