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

时间:2015-04-23 07:44:11

标签: javascript datetime

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

我有以毫秒为单位的currentTime

var currentTime = new Date().getTime()

我的futureTime以毫秒为单位

var futureTime = '1432342800000'

我想以毫秒为单位获得差异

var timeDiff = futureTime - currentTime

timeDiff

timeDiff = '2568370873'

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

有人可以帮忙吗?

8 个答案:

答案 0 :(得分:10)

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

<强> Demo

答案 1 :(得分:2)

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

答案 2 :(得分:0)

时间的差异以毫秒为单位: Get time difference between two dates in seconds

要获得差异,你必须使用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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

如果您确信该期间将始终少于一天,则可以使用这种单线:

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

N.B。如果timeDiff大于一天,这将是错误的。

答案 6 :(得分:0)

这是一个简单的函数

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

答案 7 :(得分:0)

将毫秒转换为 DD(days):HH:MM:SS

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