JavaScript实现动态时间显示功能

admin 2024-07-11 375 阅读 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>


发表评论

快捷回复: 表情:
Addoil Applause Badlaugh Bomb Coffee Fabulous Facepalm Feces Frown Heyha Insidious KeepFighting NoProb PigHead Shocked Sinistersmile Slap Social Sweat Tolaugh Watermelon Witty Wow Yeah Yellowdog
提交
评论列表 (有 0 条评论, 375人围观)