简体   繁体   中英

Disable previous dates of a datepicker according to selected date in javascript or jquery

I have two datepickers like startdate and enddate.If i select 18th june 2015 in startdate i have to disable previous dates in datepicker to 18thjune2015 in enddate.

Here is my code.

 $("#txtstartdate").datepicker({ minDate: 0 });

For end-date:

    var startdate = $("#txtstartdate").val();
    $("#txtenddate").datepicker({ minDate: startdate }); 

Its not working. Can anyone pls help me on this. thank you.

You need to set it using the option method on change of start date

 $("#txtstartdate").datepicker({ minDate: 0, onSelect: function(date) { $("#txtenddate").datepicker('option', 'minDate', date); } }); $("#txtenddate").datepicker({});
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script> <input id="txtstartdate" /> <input id="txtenddate" />

If you use input tag with attribute type="datetime-local" try this:

let today = new Date().toLocaleString('sv-SE').slice(0, 16).replace(' ', 'T');
document.getElementById('someId').setAttribute("min", today);

Try adding yearRange :

$("#txtenddate").datepicker({ 
    minDate: startdate,
    yearRange: '1999:2020', 
}); 

You have to use option

insted of

$("#txtenddate").datepicker({ minDate: startdate }); 

Do This:

$("#txtenddate").datepicker('option', 'minDate', date);

Change mindate in datepicker

$("#txtstartdate").datepicker({
  minDate: 0,
  onSelect: function(date) {
    $("#txtenddate").datepicker('option', 'minDate', date);
  }
});

$("#txtenddate").datepicker({});



<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>

<input id="txtstartdate" />
<input id="txtenddate" />

If minDate is not working, you can try startDate it will work definitely.

$('#datepicker4').datepicker({
  autoclose: true,
  startDate: new Date() 
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM