【JS】获取实时时间——结合DOM

Osinghong1个月前 (05-12)成果展示77
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>d24c04429</title>
</head>
<body>
    <button onclick="myTimer()">获取当前时间</button>
    <div id="timeDisplay"></div>
    <script>
        function getDate() {
            var today = new Date();
            var dayOfWeek = ["日", "一", "二", "三", "四", "五", "六"][today.getDay()];
            var dayOfMonth = today.getDate();
            var month = today.getMonth() + 1;
            var year = today.getFullYear();
            var hours = today.getHours();
            var minutes = today.getMinutes();
            var seconds = today.getSeconds();
            hours = (hours < 10 ? "0" : "") + hours;
            minutes = (minutes < 10 ? "0" : "") + minutes;
            seconds = (seconds < 10 ? "0" : "") + seconds;
            document.getElementById("timeDisplay").innerHTML = 
                "今天是" + year + "年" + month + "月" + dayOfMonth + "日 星期" +
                dayOfWeek + " " + hours + ":" + minutes + ":" + seconds;
        }
        function myTimer() {
            document.querySelector("button").style.display = "none";
            setInterval(getDate, 50);
        }
    </script>
</body>
</html>


打赏
分享给朋友:

相关文章

【JS】3.17课后练习3个月前 (03-17)
【JS】For语句练习3个月前 (03-17)
【JS】For语句进阶3个月前 (03-24)
【JS】While循环3个月前 (03-24)
【JS】倒三角形3个月前 (03-24)