【实例】WdatePicker插件的使用———— 两个对象,相互关联

描述:

当开始日期和结束日期的范围没有超过一月,不需要切成一个月,超过一月时,对应的日期才随动变成一个月前或一个月后

页面:

开始日期:

   

结束日期:

   

 

js:

function startpicker(date,enddateStr) {
    var tempStrs = enddateStr.split("-");
    var year = parseInt(tempStrs[0], 10);
    var month = parseInt(tempStrs[1], 10);
    var day = parseInt(tempStrs[2], 10);
    var enddate = new Date(year,month-1,day,0,0,0);

    var Y = date.getP('y');
    var M = date.getP('M')-1;
    var D = date.getP('d');
    var startdate = new Date();
    startdate.setFullYear(Y, M, D);

    if ((enddate - startdate) / (1000 * 60 * 60 * 24) < 30 && (enddate - startdate) / (1000 * 60 * 60 * 24) >0) {
        if (enddate != null) {
            if (month>=1&&month<=9) {
                month = "0" + month;
            }
            if (day >= 1 && day <= 9) {
                day="0"+day
            }
            return year + "-" + month + "-" + day;
        } else {
            return null;
        }
    } else {
        M = parseInt(M, 10) + 1;
        D = parseInt(D, 10);
        startdate.setFullYear(Y, M, D);
        var startdateMonth = startdate.getMonth() + 1;
        var startdateDay = startdate.getDate();
        if (startdateMonth >= 1 && startdateMonth <= 9) {
            startdateMonth = "0" + startdateMonth;
        }
        if (startdateDay >= 1 && startdateDay <= 9) {
            startdateDay = "0" + startdateDay;
        }
        return startdate.getFullYear() + "-" + startdateMonth + "-" + startdateDay;
    }

}
function endpicker(date,startdateStr) {
    var tempStrs = startdateStr.split("-");
    var year = parseInt(tempStrs[0], 10);
    var month = parseInt(tempStrs[1], 10);
    var day = parseInt(tempStrs[2], 10);
    var startdate = new Date(year,month-1,day,0,0,0);

    var Y = date.getP('y');
    var M = date.getP('M')-1;
    var D = date.getP('d');
    var enddate = new Date();
    enddate.setFullYear(Y, M, D);

    if ((enddate - startdate) / (1000 * 60 * 60 * 24) < 30 && (enddate - startdate) / (1000 * 60 * 60 * 24) >0) {
        if (enddate != null) {
            if (month>=1&&month<=9) {
                month = "0" + month;
            }
            if (day >= 1 && day <= 9) {
                day="0"+day
            }
            return year + "-" + month + "-" + day;
        } else {
            return null;
        }
    } else {
        M = parseInt(M, 10) - 1;
        D = parseInt(D, 10);
        enddate.setFullYear(Y, M, D);
        var enddateMonth = enddate.getMonth() + 1;
        var enddateDay = enddate.getDate();
        if (enddateMonth >= 1 && enddateMonth <= 9) {
            enddateMonth = "0" + enddateMonth;
        }
        if (enddateDay >= 1 && enddateDay <= 9) {
            enddateDay = "0" + enddateDay;
        }
        return enddate.getFullYear() + "-" + enddateMonth + "-" +enddateDay;
    }

}

你可能感兴趣的:(js,wdatepicker,时间插件,联动)