如何在 JavaScript 中将时间以毫秒为单位转换为小时、分钟、秒格式?

IT技术 javascript datetime milliseconds
2021-01-21 17:39:12

我有一个毫秒数的时间,我想将它转换为一种HH:MM:SS格式。它应该环绕,milliseconds = 86400000我想得到00:00:00.

6个回答

如何创建这样的函数:

function msToTime(duration) {
  var milliseconds = Math.floor((duration % 1000) / 100),
    seconds = Math.floor((duration / 1000) % 60),
    minutes = Math.floor((duration / (1000 * 60)) % 60),
    hours = Math.floor((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;
}
console.log(msToTime(300000))

var s = st_date+' '+start; var e = end_date+' '+end; var bits = s.split(/\D/); var bits1 = e.split(/\D/); var date = new Date(bits[0], --bits[1], bits[2], bits[3], bits[4],bits[5]); var date1 = new Date(bits1[0], --bits1[1], bits1[2], bits1[3], bits1[4],bits1[5]);
2021-03-18 17:39:12
var t1=date.getTime() var t2=date1.getTime() var t3=date1.getTime()-date.getTime(); var 秒;无功分钟;var 毫秒 = parseInt((t3%1000)/100) , seconds = parseInt((t3/1000)%60) , 分钟 = parseInt((t3/(1000*60))%60) , hours = parseInt((t3) /(1000*60*60))%24); 小时 = (小时 < 10) ? “0”+ 小时:小时;分钟 = (分钟 < 10) ? "0" + 分钟:分钟;秒 = (秒 < 10) ? "0" + 秒:秒;持续时间=小时 + ":" + 分钟 + ":" + 秒;alert('dur::'+t3); 警报('持续时间:'+持续时间);
2021-03-26 17:39:12
我已经使用这种方式将毫秒转换为小时:分钟:秒格式。但我没有正确得到它。它只为所有毫秒提供 21:11:05。我该如何实现?
2021-03-28 17:39:12
应该 var milliseconds = Math.floor(duration % 1000),
2021-04-03 17:39:12
如果持续时间输入参数已经以毫秒为单位,为什么要除以 100 得到毫秒?不应该只是 parseInt(duration % 1000) 吗?
2021-04-07 17:39:12

将时间以毫秒为单位转换为人类可读的格式。

function msToTime(ms) {
  let seconds = (ms / 1000).toFixed(1);
  let minutes = (ms / (1000 * 60)).toFixed(1);
  let hours = (ms / (1000 * 60 * 60)).toFixed(1);
  let days = (ms / (1000 * 60 * 60 * 24)).toFixed(1);
  if (seconds < 60) return seconds + " Sec";
  else if (minutes < 60) return minutes + " Min";
  else if (hours < 24) return hours + " Hrs";
  else return days + " Days"
}

console.log(msToTime(1000))
console.log(msToTime(10000))
console.log(msToTime(300000))
console.log(msToTime(3600000))
console.log(msToTime(86400000))

“输出样品”

我发现这是最有用的,在我的测试中,最佳答案似乎不能很好地处理数小时,这也为您提供了几天的选择。
2021-03-26 17:39:12
极好的!谢谢你。
2021-03-30 17:39:12
对我来说,这个答案没有帮助。“3.7”小时是什么意思???
2021-04-10 17:39:12

我遇到了同样的问题,这就是我最终做的:

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

这是我的解决方案

let h,m,s;
h = Math.floor(timeInMiliseconds/1000/60/60);
m = Math.floor((timeInMiliseconds/1000/60/60 - h)*60);
s = Math.floor(((timeInMiliseconds/1000/60/60 - h)*60 - m)*60);

// 获取时间格式 00:00:00

s < 10 ? s = `0${s}`: s = `${s}`
m < 10 ? m = `0${m}`: m = `${m}`
h < 10 ? h = `0${h}`: h = `${h}`


console.log(`${s}:${m}:${h}`);
我将值设置为三元运算符之外的 s : s = `${s < 10 ? '0': ''}${s}`
2021-03-30 17:39:12

这个像youtube视频一样返回时间

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

输出:

  • getYoutubeLikeToDisplay(129900) = "2:10"
  • getYoutubeLikeToDisplay(1229900) = "20:30"
  • getYoutubeLikeToDisplay(21229900) = "05:53:50"
评论 - 例如在负时间不能正常工作-3000应该显示-00:03但它返回-1:0-03
2021-04-08 17:39:12
我做了一些改进/调整stackoverflow.com/a/67462589
2021-04-10 17:39:12