2个具有最小和最大日期的从属日期选择器

时间:2019-01-07 15:56:10

标签: javascript jquery jquery-ui datepicker

我正在寻找一个带有2个日期选择器的表单。选择的第一个数据必须是第二个数据的minDate,第二个数据的maxDate必须是minDate + 10天。

例如:

  • 首次选择日期:01/01/2019
  • 在第二个字段中,我可以选择最多将01/10/2019作为日期

这是我刚才完成的小代码:

$(document).ready(function () {

    $("#from").datepicker({
        dateFormat: "dd-mm-yy",
        minDate: 0,
        onSelect: function (date) {
            var date2 = $('#from').datepicker('getDate');
            date2.setDate(date2.getDate() + 10);
            $('#to').datepicker('setDate', date2);
            //sets minDate to from date + 1
            $('#to').datepicker('option', 'minDate', date2);
        }
    });
    $('#to').datepicker({
        dateFormat: "dd-mm-yy",
        onClose: function () {
            var from = $('#from').datepicker('getDate');
            console.log(from);
            var to = $('#to').datepicker('getDate');
            if (to <= from) {
                var minDate = $('#to').datepicker('option', 'maxDate');
                $('#to').datepicker('setDate', minDate);
            }
        }
    });
});

3 个答案:

答案 0 :(得分:1)

如果要将to日期设置为从所选日期的+1天到距所选日期的+10天,这应该可行:

 $("#from").datepicker({
    dateFormat: "dd-mm-yy",
    minDate: 0,
    onSelect: function (date) {
        var selectedDate = $('#from').datepicker('getDate').getDate();
        var minDate = new Date();
        // set min date
        minDate.setDate(selectedDate  + 1);
        var maxDate = new Date();
        // set max date
        maxDate.setDate(selectedDate  + 10);
        $('#to').datepicker('setDate', minDate );
        //sets minDate to from date + 1
        $('#to').datepicker('option', 'minDate', minDate );
        //sets maxDate to from date + 10
        $('#to').datepicker('option', 'maxDate', maxDate );
    }
});

答案 1 :(得分:1)

尝试一下

$("#from_date").datepicker({
    format: 'yyyy/mm/dd',
    autoclose: true, 
    }).on('changeDate', function (selected) {
        var minDate = new Date(selected.date.valueOf());
        var someDate = new Date(selected.date.valueOf());
        var numberOfDaysToAdd = 10;
	someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
        var dd = someDate.getDate();
	var mm = someDate.getMonth() + 1;
	var y = someDate.getFullYear();
	var someFormattedDate = y + '/'+ mm + '/'+ dd;

        $('#to_date').datepicker('setStartDate', minDate);
        $('#to_date').datepicker('setEndDate', someFormattedDate);
    });

$("#to_date").datepicker({     
    format: 'yyyy/mm/dd',
    autoclose: true,
    }).on('changeDate', function (selected) {
        var maxDate = new Date(selected.date.valueOf());
        var someDate = new Date(selected.date.valueOf());
        var numberOfDaysToAdd = 10;
	someDate.setDate(someDate.getDate() - numberOfDaysToAdd);
	var dd = someDate.getDate();
	var mm = someDate.getMonth() + 1;
	var y = someDate.getFullYear();
	var someFormattedDate = y + '/'+ mm + '/'+ dd;
        $('#from_date').datepicker('setStartDate', someFormattedDate);
        $('#from_date').datepicker('setEndDate', maxDate);
    });
<!-- Css Link -->
<link href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />
<!-- js Link-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>

From Date:-
<input type="text" name="" class="form-control" autocomplete="off" id="from_date" placeholder="Select Date">
<br>
<br>
To Date:-
<input type="text" name="" class="form-control" autocomplete="off" id="to_date" placeholder="Select Date">

答案 2 :(得分:0)

感谢GiuServ,我找到了解决方案。

代码在这里

$(document).ready(function () {
    $("#from").datepicker({
        dateFormat: "dd-mm-yy",
        minDate: 0,
        onSelect: function (date) {
            var date2 = $('#from').datepicker('getDate');
            var minDate = new Date();
            // set min date
            minDate.setDate(date2.getDate()  + 1);
            var maxDate = new Date();
            // set max date
            maxDate.setDate(date2.getDate()  + 10);
            //sets minDate to from date + 1
            $('#to').datepicker('option', 'minDate', minDate);
            $('#to').datepicker('option', 'maxDate', maxDate);
        }
    });
    $('#to').datepicker({
        dateFormat: "dd-mm-yy",
        onClose: function () {
            var from = $('#from').datepicker('getDate');
            console.log(from);
            var to = $('#to').datepicker('getDate');
            if (to <= from) {
                var minDate = $('#to').datepicker('option', 'minDate');
                $('#to').datepicker('setDate', minDate);
                var maxDate = $('#to').datepicker('option', 'maxDate');
                $('#to').datepicker('setDate', maxDate);
            }
        }
    });
});