<script> (function (fn) { fn(); setInterval(fn, 1000); }) (function () { var dt = new Date(); const month = new Date().getMonth()+1 document.querySelector(".showTime span").innerHTML = dt.getFullYear()+ "年" + month + "月" + dt.getDate() + "日-" + dt.getHours() + "时" + dt.getMinutes() + "分" + dt.getSeconds() + "秒"; }); </script>
主要为月份从0开始,所以要加1才正确!
<div class="showTime">当前时间:<span></span></div>