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

如何将毫秒转换为“毫秒”;hhmmss“;使用javascript格式化?,javascript,datetime,Javascript,Datetime,我正在使用javascript日期对象尝试将毫秒转换为小时、分钟和秒的数量 我有以毫秒为单位的当前时间 var currentTime = new Date().getTime() var futureTime = '1432342800000' 我有以毫秒为单位的未来时间 var currentTime = new Date().getTime() var futureTime = '1432342800000' 我想得到毫秒的差值 var timeDiff = futureTime

我正在使用javascript日期对象尝试将毫秒转换为小时、分钟和秒的数量

我有以毫秒为单位的当前时间

var currentTime = new Date().getTime()
var futureTime = '1432342800000'
我有以毫秒为单位的未来时间

var currentTime = new Date().getTime()
var futureTime = '1432342800000'
我想得到毫秒的差值

var timeDiff = futureTime - currentTime
时间差是

timeDiff = '2568370873'
我想知道它是多少小时,分,秒

有人能帮忙吗

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

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

时间差以毫秒为单位:

要获得差异,必须使用math.floor()

函数msToHMS(持续时间){
var毫秒=parseInt((持续时间%1000)/100),
秒=parseInt((持续时间/1000)%60),
分钟=parseInt((持续时间/(1000*60))%60),
小时=parseInt((持续时间/(1000*60*60))%24);
小时数=(小时数<10)?“0”+小时数:小时数;
分钟=(分钟<10)?“0”+分钟:分钟;
秒=(秒<10)?“0”+秒:秒;
返回时数+“:”+分钟+“:”+秒;
}

如果您确信这段时间总是少于一天,您可以使用这一行:

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

N.B.如果
timeDiff
大于一天,这将是错误的。
将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;
}
=

这里有一个简单的函数

function simplifiedMilliseconds(milliseconds) {

  const totalSeconds = parseInt(Math.floor(milliseconds / 1000));
  const totalMinutes = parseInt(Math.floor(totalSeconds / 60));
  const totalHours = parseInt(Math.floor(totalMinutes / 60));
  const days = parseInt(Math.floor(totalHours / 24));

  const seconds = parseInt(totalSeconds % 60);
  const minutes = parseInt(totalMinutes % 60);
  const hours = parseInt(totalHours % 24);

  let time = '1s';
  if (days > 0) {
    time = `${days}d:${hours}h:${minutes}m:${seconds}s`;
  } else if (hours > 0) {
    time = `${hours}h:${minutes}m:${seconds}s`;
  } else if (minutes > 0) {
    time = `${minutes}m:${seconds}s`;
  } else if (seconds > 0) {
    time = `${seconds}s`;
  }
  return time;
}
将毫秒转换为DD(天):HH:MM:SS

函数格式化时间(timeMS){
常数[MS_IN_SEC,SEC_IN_DAY,SEC_IN_HOUR,SEC_IN_MIN]=[1000864003600,60];
设秒=数学圆(数学绝对值(timeMS)/MS(以秒为单位);
常数天数=数学下限(秒/秒/天);
秒数=数学下限(秒数%秒/天);
常数小时=数学楼层(秒/秒,单位为小时);
秒=数学地板(秒百分比秒,单位为小时);
常数分钟=数学地板(秒/秒,单位:分钟);
秒=数学地板(秒%SEC_IN_MIN);
常数[dd,hh,mm,ss]=[天,小时,分钟,秒]
.map(item=>item<10?'0'+item:item.toString());
返回dd+':'+hh+':'+mm+':'+ss;
}

一分钟60秒,一小时60分钟。我希望这是hh/mm/ss。我怎么能用这种格式呢?203秒的时间是0:0:2.34。格式不对!secDiff以秒为单位给出timeDiff。我想把整个timeDiff转换成hh:mm:ss格式。不只是将timeDiff转换为秒、分钟或小时。有什么方法可以做到这一点吗?据我所知,你必须使用timediff中已知的毫秒来建立一个新的日期。这个新的日期对象包含您需要的信息。@GijsbertBrouwer日期对象只是一个毫秒值(和一组方法),因此一旦有毫秒差,您就拥有与日期相同的数据。;-)如果您想获得超过24小时的时间,请编辑此行:hours=parseInt((duration/(1000*60*60));“。正在删除“%24”。
function simplifiedMilliseconds(milliseconds) {

  const totalSeconds = parseInt(Math.floor(milliseconds / 1000));
  const totalMinutes = parseInt(Math.floor(totalSeconds / 60));
  const totalHours = parseInt(Math.floor(totalMinutes / 60));
  const days = parseInt(Math.floor(totalHours / 24));

  const seconds = parseInt(totalSeconds % 60);
  const minutes = parseInt(totalMinutes % 60);
  const hours = parseInt(totalHours % 24);

  let time = '1s';
  if (days > 0) {
    time = `${days}d:${hours}h:${minutes}m:${seconds}s`;
  } else if (hours > 0) {
    time = `${hours}h:${minutes}m:${seconds}s`;
  } else if (minutes > 0) {
    time = `${minutes}m:${seconds}s`;
  } else if (seconds > 0) {
    time = `${seconds}s`;
  }
  return time;
}
function formatTime(timeMS) {
    const [MS_IN_SEC, SEC_IN_DAY, SEC_IN_HOUR, SEC_IN_MIN] = [1000, 86400, 3600, 60];
    let seconds = Math.round(Math.abs(timeMS) / MS_IN_SEC);
    const days = Math.floor(seconds / SEC_IN_DAY);
    seconds = Math.floor(seconds % SEC_IN_DAY);
    const hours = Math.floor(seconds / SEC_IN_HOUR);
    seconds = Math.floor(seconds % SEC_IN_HOUR);
    const minutes = Math.floor(seconds / SEC_IN_MIN);
    seconds = Math.floor(seconds % SEC_IN_MIN);
    const [dd, hh, mm, ss] = [days, hours, minutes, seconds]
        .map(item => item < 10 ? '0' + item : item.toString());
    return dd + ':' + hh + ':' + mm + ':' + ss;
}