直接上代码:
<div>距离2020年1月1日还有<h1 id="daojishi"></h1></div>
<script>
function daojishi() {
var startDate = new Date();
var endDate = new Date('1/1/2020');
var haomiao = endDate.getTime() - startDate.getTime();
var second = haomiao / 1000;
var min = second / 60;
var hour = min / 60;
var day = hour / 24;
var obj = {
day: Math.floor(day),
hour: Math.floor(hour % 24),
min: Math.floor(min % 60),
second: Math.floor(second % 60)
}
return obj;
}
setInterval(function(){
var obj = daojishi();
document.getElementById('daojishi').innerHTML = `${obj.day}天${obj.hour}时${obj.min}分${obj.second}秒`
}, 1000)
</script>