计算从当前时间到目标时间的时间差,并返回相应的天数、小时数和分钟数
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 分