How to Set minimum and maximum date dynamically in jquery UI date picker

怎甘沉沦 提交于 2021-02-07 14:28:27

问题


I have put two datepikcer from date and to date in my webpage for generate report. I want to apply validation on both datepicker for selection of date. I have write common jquery code for set minimum and maximum date of both datepikcer but it did not work for me. I have also write code for set minimum and maximum date on close of datepicker.

My requirement is that I want to set minimum and maximum date dynamically when datepicker is initialize. Then after also set another maximum and minimum date when user can select date from any datepicker

$("#frm_date").datepicker({
 showOn: 'button', 
 buttonImage: 'images/calendar.gif', 
 buttonImageOnly: true,
 dateFormat:'yy-mm-dd',
 onClose: function( selectedDate ) {
                            $( "#to_date" ).datepicker( "option", "minDate", selectedDate );
               }
});


 $("#to_date").datepicker({
 showOn: 'button', 
 buttonImage: 'images/calendar.gif', 
 buttonImageOnly: true,
 dateFormat:'yy-mm-dd',
 onClose: function( selectedDate ) {
                      $("#frm_date" ).datepicker( "option", "maxDate", selectedDate );
                   }
 });

$(".datepick").datepicker({dateFormat:'yy-mm-dd',minDate:'2013-09-10' ,maxDate:'2013-10-10'});

回答1:


I have solved my problem using below code.
Also see below solution on jquery forum site. https://forum.jquery.com/topic/how-to-set-minimum-and-maximum-date-dynamically-in-jquery-ui-date-picker

$.datepicker.setDefaults({
          showOn: 'button', 
          buttonImage: 'images/calendar.gif', 
          buttonImageOnly: true,
          dateFormat: 'yy-mm-dd',
          minDate: '2013-09-10',
          maxDate: '2013-10-10'
    });
    $('#frm_date').datepicker({
          onSelect: function(selectedDate) {
                $('#to_date').datepicker('option', 'minDate', selectedDate || '2013-09-10');
          }
    });
    $('#to_date').datepicker({
          onSelect: function(selectedDate) {
                $('#frm_date').datepicker('option', 'maxDate', selectedDate || '2013-10-10');
          }
    });

    $(".datepick").datepicker({dateFormat:'yy-mm-dd',minDate:'2013-09-10' ,maxDate:'2013-10-10'});



回答2:


You can call beforeShow method to set any option just before calendar is shown:

$("#to_date").datepicker({
    beforeShow:function(){
        $(this).datepicker('option',
                            {
                                minDate:new Date()
                             }
                          );
    }
});


来源:https://stackoverflow.com/questions/19310304/how-to-set-minimum-and-maximum-date-dynamically-in-jquery-ui-date-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!