js将秒转化为时分秒

  1. function formateTime(time) {
  2. const h = parseInt(time / 3600)
  3. const minute = parseInt(time / 60 % 60)
  4. const second = Math.ceil(time % 60)
  5. const hours = h < 10 ? '0' + h : h
  6. const formatSecond = second > 59 ? 59 : second
  7. return `${hours > 0 ? `${hours}:` : ''}${minute < 10 ? '0' + minute : minute}:${formatSecond < 10 ? '0' + formatSecond : formatSecond}`
  8. }