JavaScript实现动态时间显示功能

admin admin 2024-07-11 423 阅读 0 评论

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>显示时间</title>

</head>

<body onload="showTime()">

<script>

    function showTime() {

        var date = new Date();

        var year = date.getFullYear();

        var month = date.getMonth() + 1;

        month = month < 10 ? "0" + month : month;

        var day = date.getDate();

        day = day < 10 ? "0" + day : day;

        var week = "日一二三四五六".charAt(date.getDay()); // 使用charAt函数提取相应汉字

        var hour = date.getHours();

        hour = hour < 10 ? "0" + hour : hour; // 用三目运算符调整数字显示格式

        var minute = date.getMinutes();

        minute = minute < 10 ? "0" + minute : minute;

        var second = date.getSeconds();

        second = second < 10 ? "0" + second : second;

        var current = year + "-" + month + "-" + day + " " + "星期" + week + " " + hour + ":" + minute + ":" + second;

        document.getElementById("time").innerHTML = current;

    }

    setInterval("showTime()", 1000);

</script>

<span id="time"></span>

</body>

</html>


上一篇 下一篇

相关阅读

发表评论

访客 访客
快捷回复: 表情:
评论列表 (有 0 条评论,423人围观)