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

后端 未结 16 2382
渐次进展
渐次进展 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:24

    I had the same problem, this is what I ended up doing:

    function parseMillisecondsIntoReadableTime(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 + ':' + m + ':' + s;
    }
    
    
    var time = parseMillisecondsIntoReadableTime(86400000);
    
    alert(time);

提交回复
热议问题