关键js代码
<script> function countdown() { var targetDate = new Date("2024-02-20 12:00:00"); var currentDate = new Date(); var timeDiff = targetDate.getTime() - currentDate.getTime(); var days = Math.floor(timeDiff / (1000 * 60 * 60 * 24)); var hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((timeDiff % (1000 * 60)) / 1000); let time = `距离2024-01-15 12:00:00还有:${days}天${ hours}时${minutes}分${seconds}秒 `; document.querySelector('#div').innerHTML = time; } countdown() setInterval(function () { countdown() }, 1000); </script>
美化css代码
<style> #div { width: 800px; height: 100px; margin: 200px auto; background-color: black; color: #fff; font-size: 30px; text-align: center; line-height: 100px; border-radius: 20px; } </style>
运行效果: