Jquery UI日期选择器日期范围6个月限制

时间:2012-04-25 07:27:52

标签: jquery-ui datepicker

我正在使用jQuery ui日期选择器(日期范围)我想将其限制为6个月

var dates = $("#availability_date_from, #availability_date_to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        minDate:'0',

        yearRange:'c-18:c',

        onSelect: function( selectedDate ) {

            var option = this.id == "availability_date_from" ? "minDate" : "maxDate",

                instance = $( this ).data( "datepicker" ),

                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );


            if(this.id == "availability_date_to"){

                commonJSComplete(this.value,'availability_date_to');
                $('#responsecontainer').load('profilemeter.php');
            }
            else if(this.id == "availability_date_from"){
                commonJSComplete(this.value,'availability_date_from');
            }

        }

    });

2 个答案:

答案 0 :(得分:9)

尝试:


var dates = $("#availability_date_from, #availability_date_to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        minDate:'0',
        maxDate: '+6m',   //add this
..... 

答案 1 :(得分:1)

您可以从所选日期获取月/日/年,然后相应地更改值。我需要一个条件,用户只需要从日期选择器中选择最多6个月。因此,当选择to_date时,我会将minimun从date设置为比所选to_date低6个月的值。试试这个让我知道:))

$( "#txtFromDate" ).datepicker({
    dateFormat: 'yy-mm-dd',
    changeMonth: true,
    changeYear: true,
    onSelect: function( selectedDate ) {
        $( "#txtToDate" ).datepicker( "option", "minDate", selectedDate );
    }
});
$( "#txtToDate" ).datepicker({
    dateFormat: 'yy-mm-dd',
    changeMonth: true,
    changeYear: true,
    onSelect: function( selectedDate , instance) {

        var minDate = $.datepicker.parseDate(instance.settings.dateFormat, selectedDate, instance.settings)
        minDate.setMonth(minDate.getMonth() - 6);
        $( "#txtFromDate" ).datepicker( "option", "minDate", minDate );
    }
});