jQuery UI Picking a start and end date within range based on start date

后端 未结 3 1380
深忆病人
深忆病人 2021-02-13 13:39

I\'m building a date picker in jQuery ui. What I\'m trying to do is set a range, so when they pick the first date the second date appears and gives a 30 day window. I tried this

相关标签:
3条回答
  • 2021-02-13 14:19
    $("#start_date").datepicker().on('changeDate', function(selected){
            startDate = new Date(selected.date.valueOf());
            $('#end_date').datepicker('setStartDate', startDate);
        });  
    
        $("#end_date").datepicker().on('changeDate', function(selected){
            startDate = new Date(selected.date.valueOf());
            $('#start_date').datepicker('setEndDate', startDate);
        });
    
    0 讨论(0)
  • 2021-02-13 14:20

    Check this jsfiddle here. It's a working example of your problem.

    HTML

    <input type="text" id="dt1">
    <input type="text" id="dt2">
    

    JS

    $(document).ready(function () {
        $("#dt1").datepicker({
            dateFormat: "dd-M-yy",
            minDate: 0,
            onSelect: function () {
                var dt2 = $('#dt2');
                var startDate = $(this).datepicker('getDate');
                //add 30 days to selected date
                startDate.setDate(startDate.getDate() + 30);
                var minDate = $(this).datepicker('getDate');
                var dt2Date = dt2.datepicker('getDate');
                //difference in days. 86400 seconds in day, 1000 ms in second
                var dateDiff = (dt2Date - minDate)/(86400 * 1000);
    
                //dt2 not set or dt1 date is greater than dt2 date
                if (dt2Date == null || dateDiff < 0) {
                        dt2.datepicker('setDate', minDate);
                }
                //dt1 date is 30 days under dt2 date
                else if (dateDiff > 30){
                        dt2.datepicker('setDate', startDate);
                }
                //sets dt2 maxDate to the last day of 30 days window
                dt2.datepicker('option', 'maxDate', startDate);
                //first day which can be selected in dt2 is selected date in dt1
                dt2.datepicker('option', 'minDate', minDate);
            }
        });
        $('#dt2').datepicker({
            dateFormat: "dd-M-yy",
            minDate: 0
        });
    });
    

    As soderslatt already mentioned use the onSelect option to set the dates. Other methods I used are:

    • getDate
    • setDate
    • minDate
    • maxDate

    I think they're all very self explanatory and the documentation helps you to understand how they work. If you want to set the date of the second datepicker to dt1's date + 1 day do the same as in this line:

    startDate.setDate(startDate.getDate() + 30);
    

    But of course add 1 day and not 30.

    0 讨论(0)
  • 2021-02-13 14:24

    Try using the ui onSelect callback instead of .change(), http://api.jqueryui.com/datepicker/#option-onSelect

    on init:

    var $datepickerStart = $("#datepickerStart");
    $datepickerStart.datepicker({
        onSelect: function( selectedDate ) {
            $datepickerStart.datepicker( "option", "minDate", selectedDate );
        }
    });
    
    0 讨论(0)
提交回复
热议问题