最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jul 3, 2015 at 10:54 PHP Worm... 4,2241 gold badge27 silver badges48 bronze badges asked Jul 3, 2015 at 10:17 PriyaPriya 331 gold badge1 silver badge6 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 11

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() 
 });
发布评论

评论列表(0)

  1. 暂无评论