如何在datepicker中设置minDate

时间:2014-04-07 09:55:58

标签: jquery datepicker

以下是check_in和check_out

<input type="text" id="check_in" name="check_in" value="Thursday, 17 Apr, 2014">
<input type="text" id="check_out" name="check_out" value="Friday, 18 Apr, 2014">

这是我使用的日期选择器

$('#check_in').datepicker({
    minDate: 0,
    maxDate: '1Y',
    defaultDate:'+1d',
    numberOfMonths:2,
    dateFormat:'DD, d M, yy',
    onSelect: function(date){         
        var startDate = $('#check_in').datepicker('getDate');
        var date = new Date( Date.parse( startDate ) );
        date.setDate( date.getDate() + 1 );
        var newDate = date.toDateString();
        newDate = new Date( Date.parse( newDate ) );
        $('#check_out').datepicker('option','minDate',newDate);
        startDate.setDate(startDate.getDate() + 30);
        $('#check_out').datepicker('option', 'maxDate', startDate);
    }
});

$('#check_out').datepicker({
    minDate: '1D',
    maxDate: '30D',
    defaultDate:'+1d',
    numberOfMonths:2,
    dateFormat:'DD, d M, yy'            
});

在我点击check_out datepicker的页面加载中,&#39; minDate&#39;不是从4月18日开始。我从check_in datepicker中选择了其工作正常的4月17日

当我直接去check_out datepicker时,我如何根据check_in日期设置minDate?

1 个答案:

答案 0 :(得分:1)

试试这个,

var oneD=24*60*60*1000;
t1=new Date($('#check_in').val()).getTime();
t2=new Date().getTime();
var diffDays = Math.round((t1 - t2)/(oneD))+2;
$('#check_out').datepicker({
    minDate: diffDays+'D',
    maxDate: '30D',
    defaultDate:'+1d',
    numberOfMonths:2,
    dateFormat:'DD, d M, yy'            
});

<强> Live Updated Demo