js实现将秒数格式化为HH:MM:SS的形式

function formatSeconds(value) {
  let secondTime = parseInt(value);
  let minuteTime = 0;
  let hourTime = 0;
  if (secondTime >= 60) {
    minuteTime = parseInt(secondTime / 60);
    secondTime = parseInt(secondTime % 60);
    if (minuteTime >= 60) {
      hourTime = parseInt(minuteTime / 60);
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  // 补0
  hourTime = hourTime < 10 ? "0" + hourTime : hourTime;
  minuteTime = minuteTime < 10 ? "0" + minuteTime : minuteTime;
  secondTime = secondTime < 10 ? "0" + secondTime : secondTime;
  let res = hourTime + ":" + minuteTime + ":" + secondTime;
  return res;
}

let value = 23453;
console.log(formatSeconds(value));

 

参考文章:

https://blog.csdn.net/weixin_44109827/article/details/126397196

posted on 2023-04-07 23:54  Mikasa-Ackerman  阅读(180)  评论(0编辑  收藏  举报

导航