代码:
<script>
function countDown(time) {
var time1 = +new Date();//当前时间
var time2 = +new Date(time);//输入的时间
var result = (time2 - time1) / 1000;
var day = parseInt(result / 60 / 60 / 24);//天
day = day < 10 ? '0' + day : day;
var hour = parseInt(result / 60 / 60 % 24);//时
hour = hour < 10 ? '0' + hour : hour;
var m = parseInt(result / 60 % 60);//分
m = m < 10 ? '0' + m : m;
var s = parseInt(result % 60);//秒
s = s < 10 ? '0' + s : s;
console.log('距离开始还有'+day+'天'+hour+'时'+m+'分'+s+'秒');
}
countDown('2023-3-6 18:0:0');//输入的时间
</script>
结果图:
原理就是这样睿思拜