js实时显示当前时间(转载)

发布时间 2023-12-13 13:56:40作者: codingly
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>显示时间</title>
</head>
<body>
    <div id="time"></div>
    <script>
        function showTime() {
            var date = new Date();

            // 年月日
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();

            // 时分秒
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();

            // 实时显示
            var element = document.getElementById('time');
            element.innerHTML = '<h1>' + year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        }

        setInterval('showTime()', 1000);
    </script>
</body>
</html>

  原文出处:https://blog.csdn.net/HerryDong/article/details/103530019