【JavaScript】时分秒与秒数的转换

时分秒与秒数的转换

时间戳 格式化为 时分秒(00:00:00)

/**
 * 时间秒数格式化
 * @param s 时间戳(单位:秒)
 * @returns {*} 格式化后的时分秒
 */
let sec_to_time = function (s) {
  let t
  if (s > -1) {
    let hour = Math.floor(s / 3600)
    let min = Math.floor(s / 60) % 60
    let sec = s % 60
    if (hour < 10) {
      t = '0' + hour + ':'
    } else {
      t = hour + ':'
    }
    if (min < 10) {
      t += '0'
    }
    t += min + ':'
    if (sec < 10) {
      t += '0'
    }
    t += sec.toFixed(2)
  }
  return t
}

时分秒(00:00:00) 转为 时间戳

/**
 * 时间转为秒
 * @param time 时间(00:00:00)
 * @returns {string} 时间戳(单位:秒)
 */
let time_to_sec = function (time) {
  let s = ''
  let hour = time.split(':')[0]
  let min = time.split(':')[1]
  let sec = time.split(':')[2]
  s = Number(hour * 3600) + Number(min * 60) + Number(sec)
  return s
}
posted @ 2021-02-10 14:06  [ABing]  阅读(177)  评论(0编辑  收藏  举报