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

function secondToTimeStr(t) {
  if (!t) return;
  if (t < 60) return "00:" + ((i = t) < 10 ? "0" + i : i);
  if (t < 3600) return "" + ((a = parseInt(t / 60)) < 10 ? "0" + a : a) + ":" + ((i = t % 60) < 10 ? "0" + i : i);
  if (3600 <= t) {
    var a, i, e = parseInt(t / 3600);
    return (e < 10 ? "0" + e : e) + ":" + ((a = parseInt(t % 3600 / 60)) < 10 ? "0" + a : a) + ":" + ((i = t % 60) < 10 ? "0" + i : i);
  }
}

使用方法:

var str = secondToTimeStr(6000);
console.log(str);

效果图:

原文地址:https://www.cnblogs.com/codedisco/p/12857288.html