html页面让时间走起来,JS自定义时间date格式

文章介绍了如何使用JavaScript在HTML页面上动态显示时间,通过自定义Date对象的format方法格式化日期,并利用setInterval函数每秒更新时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html页面动态显示时间,让时间走起来,JS自定义时间date格式


    Date.prototype.format = function () {//给Date加上自定义属性:能格式化时间
        var MM = this.getMonth() + 1;
        if (this.getMonth() + 1 < 10) {
            MM = "0" + MM;
        }
        var dd = this.getDate();
        if (this.getDate() < 10) {
            dd = "0" + dd;
        }
        var hh = this.getHours();
        if (this.getHours() < 10) {
            hh = "0" + hh;
        }
        var mm = this.getMinutes();
        if (this.getMinutes() < 10) {
            mm = "0" + mm;
        }
        var ss = this.getSeconds();
        if (this.getSeconds() < 10) {
            ss = "0" + ss;
        }
        return this.getFullYear() + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss;
    }


    function moveDeviceDate() {
        var datestr = $("#device_passdate").text();
        if (!datestr || datestr.trim() == "") {
            return;
        }

        var date = new Date(datestr);
        datestr = new Date(date.getTime() + 1000).format();

        $("#device_passdate").text(datestr);
    }

    setInterval(function () {
        moveDeviceDate()
    }, 1000);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值