发布于 4年前

js 时分秒转秒数

function timeToSecond(time) {
  let times = time.split(':')
  let second = times.length > 0 ? parseInt(times.pop()) : 0
  if (second >= 60) throw Error('格式不正确')
  let minute = times.length > 0 ? parseInt(times.pop()) : 0
  if (minute >= 60) throw Error('格式不正确')
  let hour = times.length > 0 ? parseInt(times.pop()) : 0
  if (hour >= 24) throw Error('格式不正确')

  let secondCount = hour * 3600
  secondCount += minute * 60
  secondCount += second

  return secondCount
}

timeToSecond('01:00:00')    // 3600
timeToSecond('00:00:01')    // 1
©2020 edoou.com   京ICP备16001874号-3