How to constrain min and max dates for jQuery-ui datepicker based on another date

我只是一个虾纸丫 提交于 2019-11-30 21:58:28

问题


I would like to know how can we set the check-out date between the check-in date. Like if we select the month of June (when current month is February) and the 15th day. So it gives the authority to select the day between 30 days(30 limit). So the end date would be: 15 July. Here we have a code:

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/black-tie/jquery-ui.css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<style type="text/css">
body{ font: 60.0% "Trebuchet MS", sans-serif; margin: 50px; }
</style>
<script type="text/javascript">
    $(function() {
        $('#from').datepicker({
            numberOfMonths: 2,
            firstDay: 1,
            dateFormat: 'DD dd-mm-yy',
            minDate: '0',
            maxDate: '+2Y',
            onSelect: function(dateStr) {
                var min = $(this).datepicker('getDate');
                $('#to').datepicker('option', 'minDate', min || '0');
                datepicked();
            } 
        });
        $('#to').datepicker({
            numberOfMonths: 2,
            firstDay: 1,
            dateFormat: 'DD dd-mm-yy',
            minDate: '0',
            maxDate: '+2Y',
            onSelect: function(dateStr) {
                var max = $(this).datepicker('getDate');
                $('#from').datepicker('option', 'maxDate', max || '+2Y');
                datepicked();
            } 
        });
    });
    var datepicked = function() {
        var from = $('#from');
        var to = $('#to');
        var nights = $('#nights');
        var fromDate = from.datepicker('getDate')
        var toDate = to.datepicker('getDate')
        if (toDate && fromDate) {
            var difference = 0;
            var oneDay = 1000 * 60 * 60 * 24;
            var difference = Math.ceil((toDate.getTime() - fromDate.getTime()) / oneDay);
            nights.val(difference);
        }
    }
</script>
<input type="text" id="from" name="from" size="28" style="width:194px; /*Tag Style*/" value="" >
<input type="text" id="to" name="to" size="28" style="width:194px; /*Tag Style*/" value="" >
<input type="text" id="nights" name="nights" size="4" style="width:50px; /*Tag Style*/" value="" readonly="readonly">

Our Main motive is to make this code, for check-in, check out and nights. In which check-in dates can be any month in 2012-2013, and Check - out date will only be the check-in date after 30 days, and the total nights will be 30 nights only.


回答1:


Have a look at this demo. I've added some lines in the onSelect event like so:

    onSelect: function(dateStr) {
        var d1 = $(this).datepicker("getDate");
        d1.setDate(d1.getDate() + 0); // change to + 1 if necessary
        var d2 = $(this).datepicker("getDate");
        d2.setDate(d2.getDate() + 30); // change to + 29 if necessary
        $("#to").datepicker("setDate", null);
        $("#to").datepicker("option", "minDate", d1);
        $("#to").datepicker("option", "maxDate", d2);
        datepicked();
    }

According to my understanding of lodging business, the dates should be inclusive -- i.e. June/15/2012 + 30 nights should be June/14/2012. But I've followed the example you mentioned in the first few lines of question.



来源:https://stackoverflow.com/questions/9375265/how-to-constrain-min-and-max-dates-for-jquery-ui-datepicker-based-on-another-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!