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

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

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 milliseconds like, 00:00:00

How to get it in JavaScript?

回答1:

How about doing this by creating a function in javascript as shown below:

  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 


回答2:

To Convert time in millisecond to human readable format.

 function timeConversion(millisec) {          var seconds = (millisec / 1000).toFixed(1);          var minutes = (millisec / (1000 * 60)).toFixed(1);          var hours = (millisec / (1000 * 60 * 60)).toFixed(1);          var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(1);          if (seconds 



回答3:

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);


回答4:

This one returns time like youtube videos

    function getYoutubeLikeToDisplay(millisec) {         var seconds = (millisec / 1000).toFixed(0);         var minutes = Math.floor(seconds / 60);         var hours = "";         if (minutes > 59) {             hours = Math.floor(minutes / 60);             hours = (hours >= 10) ? hours : "0" + hours;             minutes = minutes - (hours * 60);             minutes = (minutes >= 10) ? minutes : "0" + minutes;         }          seconds = Math.floor(seconds % 60);         seconds = (seconds >= 10) ? seconds : "0" + seconds;         if (hours != "") {             return hours + ":" + minutes + ":" + seconds;         }         return minutes + ":" + seconds;     } 

Output:

  • getYoutubeLikeToDisplay(129900) = "2:10"
  • getYoutubeLikeToDisplay(1229900) = "20:30"
  • getYoutubeLikeToDisplay(21229900) = "05:53:50"


回答5:

my solution

var sunriseMills = 1517573074000;         // sunrise in NewYork on Feb 3, 2018  - UTC time var offsetCityMills = -5 * 3600 * 1000;   // NewYork delay to UTC  var offsetDeviceMills =  new Date().getTimezoneOffset() * 60 * 1000 ;  // eg. I live in Romania (UTC+2) >> getTimezoneOffset() = 120  var textTime = new Date(sunriseMills + offsetCityMills + offsetDeviceMills)      .toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric' }); 

textTime will become '7.04 AM'



回答6:

Based on @Chand answer. This is the implementation in Typescript. A bit safer than coercing types in JS. If you remove the type annotation should be valid JS. Also using new string functions to normalise the time.

function displayTime(millisec: number) {  const normalizeTime = (time: string): string => (time.length === 1) ? time.padStart(2, '0') : time;   let seconds: string = (millisec / 1000).toFixed(0);  let minutes: string = Math.floor(parseInt(seconds) / 60).toString();  let hours: string = '';   if (parseInt(minutes) > 59) {    hours = normalizeTime(Math.floor(parseInt(minutes) / 60).toString());    minutes = normalizeTime((parseInt(minutes) - (parseInt(hours) * 60)).toString());  }  seconds = normalizeTime(Math.floor(parseInt(seconds) % 60).toString());   if (hours !== '') {     return `${hours}:${minutes}:${seconds}`;  }    return `${minutes}:${seconds}`; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!