How to disable future dates in datepicker using jquery?

我的未来我决定 提交于 2021-02-08 11:34:48

问题


Datepicker future dates not disable. Here is my code for datepicker in HTML

<div class="input-group input-append date" id="dp3" data-date="" data-date-format="mm-dd-yyyy">
    <input class="form-control" style="width: 106px; height: 30px;" type="text" id="nodate" value="<?php echo date('m-j-Y');?>"  readonly=""/>
        <div class="input-group-addon add-on" style="width: 35px; height: 25px;"><i class="icon-calendar"></i></div>
        &emsp;
        <button type="button" class="btn btn-primary" onclick="window.location = '<?php echo base_url().'index.php/attendance/noRecordDate/';?>'+ $('#nodate').val()">Add Attendance</button>
</div>

Here is my script:

<script>
    $(function() {
    $( "#nodate" ).datepicker({  maxDate: new Date() });
});
</script>

回答1:


maxDate

Multiple types supported:

Date: A date object containing the maximum date.
Number: A number of days from today. For example 2 represents two days from today and -1 represents yesterday.
String: A string in the format defined by the dateFormat option, or a relative date. Relative dates must contain value and period pairs; valid periods are "y" for years, "m" for months, "w" for weeks, and "d" for days. For example, "+1m +7d" represents one month and seven days from today.

so you need to either set the date object properly , or simply set as string or number as follows:

<script>
    $(function() {
    $( "#nodate" ).datepicker({  maxDate: '0' });
});
</script>



回答2:


$(document).ready(function () {

    var dbDate = "2017-03-24";
    var date2 = new Date(dbDate);

    $( "#nodate" ).datepicker({ 
    
      
     dateFormat: 'mm-dd-yy',
     maxDate: new Date()
    
    }).datepicker('setDate', date2);

    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


<div class="input-group input-append date" id="dp3" data-date="" data-date-format="mm-dd-yyyy">
    <input class="form-control" style="width: 106px; height: 30px;" type="text" id="nodate" value=""  readonly=""/>
        <div class="input-group-addon add-on" style="width: 35px; height: 25px;"><i class="icon-calendar"></i></div>
        &emsp;
        <button type="button" class="btn btn-primary" onclick="window.location = '<?php echo base_url().'index.php/attendance/noRecordDate/';?>'+ $('#nodate').val()">Add Attendance</button>
</div>


来源:https://stackoverflow.com/questions/43017024/how-to-disable-future-dates-in-datepicker-using-jquery

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