javascript 如何使用javascript将毫秒转换为“hhmmss”格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29816872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:07:08  来源:igfitidea点击:

How can I convert milliseconds to "hhmmss" format using javascript?

javascriptdatetime

提问by haeminish

I am using javascript Date object trying to convert millisecond to how many hour, minute and second it is.

我正在使用 javascript Date 对象试图将毫秒转换为它是多少小时、分钟和秒。

I have the currentTime in milliseconds

我有以毫秒为单位的 currentTime

var currentTime = new Date().getTime()

and I have futureTime in milliseconds

我有以毫秒为单位的未来时间

var futureTime = '1432342800000'

I wanted to get difference in millisecond

我想得到毫秒的差异

var timeDiff = futureTime - currentTime

the the timeDiff was

timeDiff 是

timeDiff = '2568370873'

I want to know how many hours, minutes, seconds it is.

我想知道它是多少小时,分钟,秒。

Could anyone help?

有人可以帮忙吗?

回答by ozil

var secDiff = timeDiff / 1000; //in s
var minDiff = timeDiff / 60 / 1000; //in minutes
var hDiff = timeDiff / 3600 / 1000; //in hours  

updated

更新

function msToHMS( ms ) {
    // 1- Convert to seconds:
    var seconds = ms / 1000;
    // 2- Extract hours:
    var hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
    seconds = seconds % 3600; // seconds remaining after extracting hours
    // 3- Extract minutes:
    var minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
    // 4- Keep only seconds not extracted to minutes:
    seconds = seconds % 60;
    alert( hours+":"+minutes+":"+seconds);
}

var timespan = 2568370873; 
msToHMS( timespan );  

Demo

演示

回答by Mamunur Rashid

Convert ms to hh:mm:ss

将 ms 转换为 hh:mm:ss

function millisecondsToHuman(ms) {
  const seconds = Math.floor((ms / 1000) % 60);
  const minutes = Math.floor((ms / 1000 / 60) % 60);
  const hours = Math.floor((ms  / 1000 / 3600 ) % 24)

  const humanized = [
    pad(hours.toString(), 2),
    pad(minutes.toString(), 2),
    pad(seconds.toString(), 2),
  ].join(':');

  return humanized;
}
=

回答by Palsri

function msToHMS( 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 ;
}

回答by tdbit

If you are confident that the period will always be less than a day you could use this one-liner:

如果您确信该期间将始终少于一天,您可以使用此单行:

new Date(timeDiff).toISOString().slice(11,19)   // HH:MM:SS

N.B. This will be wrong if timeDiffis greater than a day.

注意如果timeDiff大于一天,这将是错误的。

回答by Gijsbert Brouwer

The difference in time is in milliseconds: Get time difference between two dates in seconds

时间差以毫秒为单位:以秒为单位 获取两个日期之间的时间差

to get the difference you have to use math.floor() http://www.w3schools.com/jsref/jsref_floor.asp

要获得差异,您必须使用 math.floor() http://www.w3schools.com/jsref/jsref_floor.asp

var secDiff = Math.floor(timeDiff / 1000); //in s
var minDiff = Math.floor(timeDiff / 60 / 1000); //in minutes
var hDiff = Math.floor(timeDiff / 3600 / 1000); //in hours

回答by Piyush Srivastava

var timediff = futureTime - currentTime
long seconds = (long) (timediff / 1000) % 60 ;
long minutes = (long) ((timediff / (1000*60)) % 60);
long hours   = (long) ((timediff / (1000*60*60)) % 24);
if(hours>0)
    time = hours+" hrs : "+minutes+" mins";
else if(minutes>0)
    time = minutes+" mins";
else if(seconds>0)
    time = seconds+" secs";