console
function countDown(time) {
let startTime = new Date()
let endTime = new Date(time)
let leftTime = endTime.getTime() - startTime.getTime()
let days = Math.floor(leftTime / (1000 * 60 * 60 * 24)),
hours = Math.floor(leftTime / (1000 * 60 * 60) % 24),
mins = Math.floor(leftTime / (1000 * 60) % 60),
secs = Math.floor(leftTime / 1000 % 60);
return `${days}天${hours}小时${mins}分${secs}秒`
}
let end = '2021/11/11'
let time = document.getElementsByClassName('main')[0]
let tip = document.getElementsByClassName('tip')[0]
tip.innerHTML = `距离${end}还有:`
setInterval(() => {
time.innerHTML=countDown(end)
}, 1000)
<div class="time">
<div class="tip"></div>
<div class="main"></div>
</div>
.time {
width: 50%
}
.time .main {
position: relative;
width: 100%;
height: 200px;
line-height: 200px;
text-align: center;
font-size: 30px;
border: 1px solid gray;
}
.time .tip {
position: absolute;
width: 300px;
height: 20px;
margin: 0 auto;
top: 10px;
font-size: 14px
}