2

js 计算时间相差

 1 year ago
source link: http://surest.cn/archives/213/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

js 计算时间相差

Published on Nov 29, 2022 in with 0 comment
const contDownTime = value => {
  const dayDiff = Math.floor(value / (24 * 3600)) // 计算出相差天数
  const leave1 = value % (24 * 3600) // 计算天数后剩余的毫秒数
  const hours = Math.floor(leave1 / 3600) // 计算出小时数
  const leave2 = leave1 % 3600 // 计算小时数后剩余的毫秒数
  const minutes = Math.floor(leave2 / 60) // 计算相差分钟数
  const leave3 = leave2 % 60 // 计算分钟数后剩余的毫秒数
  const seconds = Math.round(leave3)
  let t = ''

  if (dayDiff > 0) {
    t += dayDiff + "天"
  }

  if (hours > 0) {
    t += hours + "小时"
  }
  if (minutes > 0) {
    t += minutes + "分钟"
  }

  if (seconds > 0) {
    t += seconds + "秒"
  }

  if (!t) {
    return false
  }
  return t
}
const gtDiffTime = (utcDateEnd, utcDateBegin, type = 'string') => {
  const dateDiff = moment(utcDateEnd).unix() - _moment_utc(utcDateBegin).unix() // 时间差的毫秒数
  if (type == 'number') {
    return dateDiff
  }
  if (dateDiff < 0) {
    return ''
  }
  return contDownTime(dateDiff)
}

本文由 邓尘锋 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Nov 29, 2022 at 01:29 pm


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK