js 时间戳转换为倒计时

js 时间戳转换为倒计时

countDown(number) {. // 传入时间戳
let timeStamp = number - Math.round(new Date().getTime() / 1000) // 传入时间戳减去当前时间戳。 除1000是为了消除毫秒
var timer = setInterval(() => { // 设置定时器 一秒更新一次数据
if (timeStamp <= 0) { // 如果到0了以后清除定时器
this.day = ‘00’
this.hour = ‘00’
this.second = ‘00’
clearInterval(timer)
}
timeStamp -= 1
// 计算 天 时 分 秒
const tempDay = Math.floor(timeStamp / (60 * 60 * 24))
const tempHour = Math.floor(timeStamp / (60 * 60)) - tempDay * 24
const tempMin = Math.floor(timeStamp / 60) - tempDay * 24 * 60 - tempHour * 60
const tempSecond = timeStamp - tempDay * 24 * 60 * 60 - tempHour * 60 * 60 - tempMin * 60
// 为了倒计时 个数和双数不影响宽度变化 做数据处理
this.day = String(tempDay).length === 1 ? ‘0’ + tempDay : tempDay
this.hour = String(tempHour).length === 1 ? ‘0’ + tempHour : tempHour
this.min = String(tempMin).length === 1 ? ‘0’ + tempMin : tempMin
this.second = String(tempSecond).length === 1 ? ‘0’ + tempSecond : tempSecond
}, 1000)
}

{{ ${day}天${hour}时${min}分 }}

{{ ${hour}时${min}分${second}秒 }}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值