Changing minDate and maxDate on the fly using jQuery DatePicker

前端 未结 5 1602
无人共我
无人共我 2020-11-28 12:12

I\'m having a particular issue with the jQuery Datepicker. I can easily add a date range, but I want the selectable range to change depending on the event the user has picke

相关标签:
5条回答
  • 2020-11-28 12:21

    I have changed min date property of date time picker by using this

    $('#date').data("DateTimePicker").minDate(startDate);
    

    I hope this one help to someone !

    0 讨论(0)
  • 2020-11-28 12:27

    I know you are using Datepicker, but for some people who are just using HTML5 input date like me, there is an example how you can do the same: JSFiddle Link

    $('#start_date').change(function(){
      var start_date = $(this).val();
      $('#end_date').prop({
        min: start_date
      });
    });
    
    
    /*  prop() method works since jquery 1.6, if you are using a previus version, you can use attr() method.*/
    
    0 讨论(0)
  • 2020-11-28 12:33

    You have a couple of options...

    1) You need to call the destroy() method not remove() so...

    $('#date').datepicker('destroy');
    

    Then call your method to recreate the datepicker object.

    2) You can update the property of the existing object via

    $('#date').datepicker('option', 'minDate', new Date(startDate));
    $('#date').datepicker('option', 'maxDate', new Date(endDate));
    

    or...

    $('#date').datepicker('option', { minDate: new Date(startDate),
                                      maxDate: new Date(endDate) });
    
    0 讨论(0)
  • 2020-11-28 12:37
    $(document).ready(function() {
    $("#aDateFrom").datepicker({
        onSelect: function() {
            //- get date from another datepicker without language dependencies
            var minDate = $('#aDateFrom').datepicker('getDate');
            $("#aDateTo").datepicker("change", { minDate: minDate });
        }
    });
    
    $("#aDateTo").datepicker({
        onSelect: function() {
            //- get date from another datepicker without language dependencies
            var maxDate = $('#aDateTo').datepicker('getDate');
            $("#aDateFrom").datepicker("change", { maxDate: maxDate });
        }
    });
    });
    
    0 讨论(0)
  • 2020-11-28 12:41

    For from / to date, here is how I implemented restricting the dates based on the date entered in the other datepicker. Works pretty good:

    function activateDatePickers() {
        $("#aDateFrom").datepicker({
            onClose: function() {
                $("#aDateTo").datepicker(
                        "change",
                        { minDate: new Date($('#aDateFrom').val()) }
                );
            }
        });
        $("#aDateTo").datepicker({
            onClose: function() {
                $("#aDateFrom").datepicker(
                        "change",
                        { maxDate: new Date($('#aDateTo').val()) }
                );
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题