时间戳转分秒 | 字符串的padStart()和padEnd()

function _pad(num, n = 2) {
  let len = num.toString().length
   while (len < n) {
   num = '0' + num
     len++
   }
   return num
}
let seconds = 8
_pad(seconds) // '08'

 

function format(interval) {
  interval = interval | 0
  let minitues = interval / 60 | 0
  let seconds = interval % 60
  return `${minitues}:${(seconds + '').padStart(2, '0')}`
}
format(123) // '2:03'

 

你可能感兴趣的:(时间戳转分秒 | 字符串的padStart()和padEnd())