minDate and maxDate options are not working in datepicker

风流意气都作罢 提交于 2019-12-24 02:25:34

问题


I'm using the "datepicker" plugin on my project, it's set to decade view, and I want to disable the future dates, for this I have used the maxDate option but it's not working, my code:

$('#data_1 .input-group.date').datepicker({
    todayBtn: "linked",
    maxDate: "0",
    keyboardNavigation: false,
    forceParse: false,
    calendarWeeks: true,
    autoclose: true
});

tried with 0 and new Date


回答1:


Note that bootstrap-datepicker has no maxDate option, you have to use endDate.

Here a working sample:

$("#datepicker").datepicker({
  todayBtn: "linked",
  endDate: new Date(),
  keyboardNavigation: false,
  forceParse: false,
  calendarWeeks: true,
  autoclose: true
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker3.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>

<input type="text" class="form-control" id="datepicker">



回答2:


Try below code:

var today = new Date();  
$('#data_1 .input-group.date').datepicker({
            todayBtn: "linked",
            endDate: "today",
            maxDate: today
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            autoclose: true
        });

Also you can try with:

 maxDate: 0



回答3:


Adding attribute data-date-end-date="0d" worked for me:

<input type="text" id="birthDay" data-date-end-date="0d">



回答4:


If u are trying calendar for start date and end date then this will help you :)

`$("#from").datepicker({
    format : 'dd-mm-yyyy',
    endDate: '<?php echo $to; ?>',
    changeMonth: false,
    changeYear: false,
    prevText: '<i class="fa fa-chevron-left"></i>',
    nextText: '<i class="fa fa-chevron-right"></i>',
    onClose: function (selectedDate) {
        $("#to").datepicker("option", "startDate", selectedDate);
    }
});
$("#to").datepicker({
    format : 'dd-mm-yyyy',
    startDate: '<?php echo $from; ?>',
    changeMonth: false,
    changeYear: false,
    prevText: '<i class="fa fa-chevron-left"></i>',
    nextText: '<i class="fa fa-chevron-right"></i>',
    onClose: function (selectedDate) {
        $("#from").datepicker("option", "endDate", selectedDate);
    }
});`


来源:https://stackoverflow.com/questions/44391206/mindate-and-maxdate-options-are-not-working-in-datepicker

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