JQuery Restrict the difference between two datepickers

后端 未结 4 1678
小鲜肉
小鲜肉 2021-01-24 09:04

I have 2 datepickers

  $(function() {
    $(\'#DateFrom\').datepicker({ onSelect: showUser, minDate: -90, maxDate: \"+1D\" });
  });

  $(function() {
    $(\'#         


        
相关标签:
4条回答
  • 2021-01-24 09:13

    Alternate suggestion:

    If you are restricting the day they can pick in the "TO" datepicker to only 1 day, then why have a second datepicker at all? Just calculate 7 days from the FROM datepicker date and display it as text. Silly to make the user pick a date when theres only one option.

    0 讨论(0)
  • 2021-01-24 09:21

    A little suggestion, try finding a JQuery Range Picker. They'll include some of these tedious functionality for you already. Going on google and searching just that gives you a ton of choices.

    etc..https://github.com/longbill/jquery-date-range-picker

    Back to the question though, you could put a callback function on 'onClose' in 'DateFrom' so that whenever the date is change, you will change the 'minDate' on 'DateTo' as well.

    0 讨论(0)
  • 2021-01-24 09:30

    Use the beforeShow parameter to modify the minDate of the To field, and then simply use date objects and a +7 to select a date one week from the From date:

    $("#to").datepicker({
        defaultDate: "+1w",  // set the default to 1 week from today
    
        beforeShow: function () {
            var date = $('#from').datepicker('getDate');  // From date
            date.setDate(date.getDate() + 7);             // 7 days after From date
            $( "#to" ).datepicker( "option", "minDate", date );  // set minDate to from+7
        }
    });
    

    Fiddle

    0 讨论(0)
  • 2021-01-24 09:33

    Something like this

    $(function () {
        $('#DateFrom').datepicker({
            onSelect: function() {
                var date = $('#DateFrom').datepicker('getDate');
                date.setTime(date.getTime() + (1000*60*60*24*6));
                $('#DateTo').datepicker('option', 'maxDate', date);
                $('#DateTo').datepicker('option', 'minDate', $('#DateFrom').datepicker('getDate'));
                showUser();
            },
            minDate: -90, 
            maxDate: "+1D"
        });
    
        $('#DateTo').datepicker({
            onSelect: showUser,
            minDate: -90, 
            maxDate: "+1D"
        });
    });
    

    FIDDLE

    0 讨论(0)
提交回复
热议问题