How to convert time milliseconds to hours, min, sec format in JavaScript?

后端 未结 16 2366
渐次进展
渐次进展 2020-11-29 02:28

I have the time as milliseconds, but I want the time after conversion like 00:00:00.

Ex: In milliseconds=86400000. I want how many hours in that millise

16条回答
  •  暖寄归人
    2020-11-29 03:12

    Worked for me

    msToTime(milliseconds) {
        //Get hours from milliseconds
        var hours = milliseconds / (1000*60*60);
        var absoluteHours = Math.floor(hours);
        var h = absoluteHours > 9 ? absoluteHours : '0' + absoluteHours;
    
        //Get remainder from hours and convert to minutes
        var minutes = (hours - absoluteHours) * 60;
        var absoluteMinutes = Math.floor(minutes);
        var m = absoluteMinutes > 9 ? absoluteMinutes : '0' +  absoluteMinutes;
    
        //Get remainder from minutes and convert to seconds
        var seconds = (minutes - absoluteMinutes) * 60;
        var absoluteSeconds = Math.floor(seconds);
        var s = absoluteSeconds > 9 ? absoluteSeconds : '0' + absoluteSeconds;
    
        return h == "00" ? m + ':' + s : h + ':' + m + ':' + s;
    }
    

提交回复
热议问题