jquery ui datepicker - how to set two min/max date restrictions in one datepicker?

后端 未结 2 2001
遥遥无期
遥遥无期 2021-01-28 16:43

I am using the jquery ui datepicker with select date range. I know that by default it already set if the from picks a date then the to date can not pick any date before the fro

2条回答
  •  野的像风
    2021-01-28 17:11

    Alright so pretty much you need to check if the selectedDate is empty when date-to-field is updated and make the maxDate to "0". Once you do it should act as you wanted, it'll set the max to today's date or if the date of the from if it's not todays date. Here's a codepen that works for me - CodePen.

        $("#date-from-field").datepicker({
      onClose: function( selectedDate ) {
          $( "#date-to-field" ).datepicker( "option", "minDate", selectedDate );
        },
      maxDate: "0"
    });
    
    $("#date-to-field").datepicker({
      onClose: function( selectedDate ) {
        $( "#date-from-field" ).datepicker( "option", "maxDate", selectedDate ? selectedDate: "0" );
        },
      maxDate: "0"
    });
    

    EDIT

    Updated the CodePen a bit more so that it checks if the selected date is greater than todays date.

    $("#date-to-field").datepicker({
      onClose: function( selectedDate ) {
        var possibleDate = new Date(selectedDate);
        possibleDate = (possibleDate < new Date())?possibleDate: new Date();
        $( "#date-from-field" ).datepicker( "option", "maxDate", selectedDate ? possibleDate: "0" );
        },
      maxDate: "0"
    });
    

提交回复
热议问题