怎么让输入开始日期不得小于当前日期三个月,比如:当前是5月份输入日期只能往前选到2月份在jsp里表单验证里

怎么让输入开始日期不得小于当前日期三个月,比如:当前是5月份输入日期只能往前选到2月份在jsp里表单验证里

问题描述:

怎么在jsp表单验证里让输入的日期不得小于当前日期三个月

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 jdate = document.getElementById("startdate").value;//开始日期
                var jedate = document.getElementById("enddate").value;//截止日期
                var d=new Date().Format("yyyy-MM-dd");//获取系统当前日期
                    //转换为数组
                    //开始日期数值
                var year=jdate.substr(0,4);
                var monty=jdate.substr(5,2);
                var day=jdate.substr(8,2); 
                //截止日期数组
                var year1=jedate.substr(0,4);
                var monty1=jedate.substr(5,2);
                var day1=jedate.substr(8,2);
                //系统当前日期数组
                var year2=d.substr(0,4);
                var monty2=d.substr(5,2);
                var day2=d.substr(8,2); 

                var a=monty2-monty;
                var b=monty-monty2;
                var c=year2-year;

                 if((year=year2&&a>2)||(c>0&&b<10)||(c>1)){  
                        alert("只能查询三个月以内的信息");
                        return;
                         }

你后面日期onchange方法里去给前面那个日期赋值,先获取后面日期的值,然后日期加上三个月,OK

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

     function dateDiff(date1, date2){     
        var type1 = typeof date1, type2 = typeof date2;     
        if(type1 == 'string')     
            date1 = stringToTime(date1);     
        else if(date1.getTime)     
            date1 = date1.getTime();     
        if(type2 == 'string')     
            date2 = stringToTime(date2);     
        else if(date2.getTime)     
            date2 = date2.getTime(); 
        return (date2 - date1) / 1000 / 60 / 60 / 24;//除1000是毫秒,不加是秒 
    } 
    //字符串转成Time(dateDiff)所需方法 
    function stringToTime(string){     
        var f = string.split(' ', 2);     
        var d = (f[0] ? f[0] : '').split('-', 3);     
        var t = (f[1] ? f[1] : '').split(':', 3);     
        return (new Date(     
        parseInt(d[0], 10) || null,     
        (parseInt(d[1], 10) || 1)-1,     
        parseInt(d[2], 10) || null,     
        parseInt(t[0], 10) || null,    
        parseInt(t[1], 10) || null,     
        parseInt(t[2], 10) || null)).getTime(); 
    }


 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 d=new Date().Format("yyyy-MM-dd");
            var jdate = document.getElementById("vc_paymentDate").value;

 if(dateDiff(jdate,d)>90){
    alert("只能查询三个月以内的数据");
 }