JavaScript动态显示时间

<body>
    <div></div>

    <script>
        var div = document.querySelector('div');
        retNowTime();
        setInterval(retNowTime, 1000);
        function retNowTime() {
            var date = new Date();
            var hours = date.getHours();
            hours = hours < 10 ? '0' + hours : hours;
            var minutes = date.getMinutes();
            minutes = minutes < 10 ? '0' + minutes : minutes;
            var seconds = date.getSeconds();
            seconds = seconds < 10 ? '0' + seconds : seconds;
            div.innerHTML = hours + ':' + minutes + ':' + seconds;
        }
    </script>
</body>

 

posted @ 2020-10-11 10:23  WP-WangPin  阅读(99)  评论(0编辑  收藏  举报