js 判断时间不能超过3个月

 /**最多提前年检三个月*/
    function checkEvent(){
        $(".nj_btn").attr("disabled", true);
        $(".update_btn").attr("disabled", true);
        $(".detail_btn").attr("disabled", true);
        var row = $("#bootstrap-table").bootstrapTable('getSelections');
        if(row.length != 1){
            return;
        }
        var jhsjrq = row[0].jhsjrqStr;
        var nowDate = new Date();

        var nowrq = nowDate.format("yyyy-MM");
        var date1 = new Date(Date.parse(jhsjrq.replace(/-/g,"/")));
        var sjrq  = date1.format("yyyy-MM");

        //判断时间跨度是否大于3个月
        var arr1 = sjrq.split('-');
        var arr2 = nowrq.split('-');
        arr1[0] = parseInt(arr1[0]);
        arr1[1] = parseInt(arr1[1]);
        arr2[0] = parseInt(arr2[0]);
        arr2[1] = parseInt(arr2[1]);
        var flag = true;
        if(arr1[0] == arr2[0]){//同年
            if(Math.abs(arr2[1]-arr1[1]) > 3){ //月间隔超过3个月
                flag = false;
            }
        }else{ //不同年
            if(arr2[1] < 10){ //开始年的月份小于10时
                flag = false;
            }else if(arr1[1] > 3){
                flag = false;
            }
        }
        if(flag){
            $(".nj_btn").attr("disabled", false);
            $(".update_btn").attr("disabled", false);
            $(".detail_btn").attr("disabled", false);
        }

    }


    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;
    }

学习:https://blog.csdn.net/huohuanyu1/article/details/76735245

你可能感兴趣的:(js)