动态获取当前日期时间

审核中 JavaScript 未结 已结 置顶 精帖
删除 置顶 取消置顶 加精 取消加精
66 0
yswl
yswl VIP3 2022-02-11 17:29:10
悬赏:60金币 编辑此贴

动态获取当前日期时间


html

<span class="index_h_c_time">2022年2月11日 17:28:05</span>


js

<script>
        function clock(){
            now = new Date();
            year = now.getFullYear();
            month = now.getMonth() + 1;
            day = now.getDate();

            hour = now.getHours();
            min = now.getMinutes();
            sec = now.getSeconds();
            if (hour < 10) {
                hour = "0" + hour;
            }
            if (min < 10) {
                min = "0" + min;
            }
            if (sec < 10) {
                sec = "0" + sec;
            }
            $(".index_h_c_time").html(
                year + "年" + month + "月" + day + "日" + " " + hour + ":" + min + ":" + sec
            );
        }
        setInterval(clock,1000);
    </script>