系统时间(蓝)

蓝色实时系统时间
<script>
    // 获取显示时间的DOM元素
    const timeElement = document.getElementById('current-time');

    // 补零函数:确保个位数显示为两位数(如 9 → 09)
    function padZero(num) {
        return num < 10 ? `0${num}` : num;
    }

    // 更新时间的核心函数
    function updateSystemTime() {
        // 获取当前系统时间
        const now = new Date();
        
        // 提取并格式化时间各部分
        const year = now.getFullYear();
        const month = padZero(now.getMonth() + 1); // 月份从0开始,需+1
        const day = padZero(now.getDate());
        const hour = padZero(now.getHours());
        const minute = padZero(now.getMinutes());
        const second = padZero(now.getSeconds());

        // 拼接完整时间格式并渲染到页面
        const timeStr = `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
        timeElement.textContent = timeStr;
    }

    // 初始化立即执行一次,避免页面加载延迟
    updateSystemTime();
    // 设置定时器,每秒更新一次时间(1000毫秒 = 1秒)
    setInterval(updateSystemTime, 1000);
</script>
posted @ 2026-03-11 22:55  潘聪  阅读(0)  评论(0)    收藏  举报