js获取当前时间前一个小时的方法分享

发布于 5 年前 作者 sinazl 5343 次浏览 最后一次编辑是 11 天前 来自 分享

js获取当前时间前一个小时的方法分享:

第一步 新建js文件,复制粘贴以下内容

$(function(){

        Date.prototype.format = function(fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份 
                "d+": this.getDate(), //日 
                        "h+": this.getHours(), //小时 
                        "m+": this.getMinutes(), //分 
                        "s+": this.getSeconds(), //秒 
                        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
                        "S": this.getMilliseconds() //毫秒 
                };
                if(/(y+)/.test(fmt)) {
                        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 -RegExp.$1.length));
                }
                for(var k in o) {
                        if(new RegExp("(" + k + ")").test(fmt)) {
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                        }
                }
                return fmt;
        }
        
        
        var now = new Date;
        now.setMinutes(now.getMinutes() - 60);
        var stime = now.format("yyyy-MM-dd hh:mm");//一个小时前的时间
        var etime = new Date().format("yyyy-MM-dd hh:mm:ss");//当前时间
        $(".hours").text(stime);
//        $(".hours").val(etime);
//        console.log(etime)
})

第二步:在html中引入 <script src=“js路径” type=“text/javascript” charset=“utf-8”></script> src路径根据你文件夹目录而定

第三步:在你需要修改的时间标签上加入class="hours"

第四步 时间修改完成

2024年最新最全HarmonyOS Next 仿小米商城App入门实战系列教程免费学习地址:http://bbs.itying.com/topic/6568836e6a4621002b88a6f6 2024年 Flutter+Getx仿小米商城项目实战视频教程-V3版网盘下载地址:http://bbs.itying.com/topic/620268d1a4bcc00fe8e9d6e1

回到顶部