将毫秒数转换为时分秒格式

199 阅读1分钟
fixedTwo(num) {
  if (num >= 0 && num <= 9) {
    num = '0' + num;
  }
  return num;
}

function formatSecondToHHmmss(time) {
  time = Number(time);
  if (time < 0) time = 0;
  let str = ''

  const secs = Math.floor(time / 1000);
  const sec = Math.floor(secs % 60);
  const mins = Math.floor(secs / 60);
  const min = Math.floor(mins % 60);
  const hour = Math.floor(mins / 60);

  str = `${fixedTwo(hour)}:${fixedTwo(min)}:${fixedTwo(sec)}`;

  return str;
};
console.log(formatSecondToHHmmss(75000)) // 00:01:15