效果图
<view class="countdown-time">0</view><view class="countdown-time">{{countdown_days}}</view><view style="margin-right: 10rpx;font-weight: bold">天</view>
<view class="countdown-time">{{countdown_hours[0]}}</view><view class="countdown-time">{{countdown_hours[1]}}</view><view style="margin-right: 10rpx;font-weight: bold">时</view>
<view class="countdown-time">{{countdown_minutes[0]}}</view><view class="countdown-time">{{countdown_minutes[1]}}</view><view style="margin-right: 10rpx;font-weight: bold">分</view>
<view class="countdown-time">{{countdown_seconds[0]}}</view><view class="countdown-time">{{countdown_seconds[1]}}</view><view style="margin-right: 10rpx;font-weight: bold">秒</view>
js
const countdown_days = ref(0)
const countdown_hours = ref([])
const countdown_minutes = ref([])
const countdown_seconds = ref([])
// 倒计时
const getCountdown = (drawTime) => {
setInterval(() => {
if(new Date().getTime() < new Date(drawTime).getTime()){
let now = new Date().getTime()
// let draw = new Date(drawTime).getTime()
let draw = new Date(drawTime).getTime()
let remain = draw - now // 现在时间与开奖时间相差毫秒数
// 天数
countdown_days.value = Math.floor(remain / 86400000)
// 小时数
let level1 = remain % 86400000
let arr1 = Array.from(String(Math.floor(level1 / 3600000)))
if(arr1.length < 2){
arr1.unshift(0)
}
countdown_hours.value = arr1
// 分钟数
let level2 = level1 % 3600000
let arr2 = Array.from(String(Math.floor(level2 / 60000)))
if(arr2.length < 2){
arr2.unshift(0)
}
countdown_minutes.value = arr2
// 秒数
let level3 = level2 % 60000
let arr3 = Array.from(String(Math.floor(level3 / 1000)))
if(arr3.length < 2){
arr3.unshift(0)
}
countdown_seconds.value = arr3
}
}, 1000)
}