计算从当前时间到目标时间的时间差,并返回相应的天数、小时数和分钟数

distanceOpenTime(showTime) {
  let timer = showTime

  const currentTime = new Date()
  const targetTime = new Date(showTime)

  // 计算时间差(以毫秒为单位)
  const timeDiff = targetTime.getTime() - currentTime.getTime()

  // 将时间差转换为小时、分钟和秒数
  const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24))
  const hours = Math.floor(timeDiff / (1000 * 60 * 60) % 24)
  const minutes = Math.floor((timeDiff / (1000 * 60)) % 60)
  const seconds = Math.floor((timeDiff / 1000) % 60)

  let ret = days >= 0 ? `${days}${hours} 小时 ${minutes}` : '已过期'

  return ret
}

例如,现在是2023-08-21 14:30,距离, ‘2023-08-25T11:00:00.000+00:00’(2023-08-25 19:00:00) ,还有 4 天 4 小时 30 分

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐