Guruprasad Rao February 2016

set maxDate as currentDate in jquery-ui datepicker

I was trying to set maxDate as currentDate but unfortunately, the year dropdown doesn't get updated. Below is what I've.

var currentDate=new Date();
var minyear=currentDate.getFullYear() - 80;
var maxyear=currentDate.getFullYear() - 1;
$('.datepicker').datepicker({
  changeMonth: true,
  changeYear: true,
  yearRange:minyear +':'+maxyear,
  showAnim:'clip'
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<input type="text" class="datepicker"/>

My try included adding setDate option with a date less than year but still dropdown value remained the minDate year. Below is what I tried:

var currentDate=new Date();
var minyear=currentDate.getFullYear() - 80;
var maxyear=currentDate.getFullYear() - 1;
var setDates=new Date(currentDate.getMonth()+"/"+currentDate.getDate()+"/"+(currentDate.getFullYear()-1));
$(".datepicker").datepicker({
      changeMonth: true,
      changeYear: true,
      yearRange:minyear +':'+maxyear,
      showAnim:'clip',
      setDate:setDates
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" rel="stylesheet"/>
<script src="http        

Answers


Anil Kumar February 2016

We cane restrict the range of selectable dates with the minDate and maxDate options. Set the beginning and end dates as actual dates (new Date(2009, 1 - 1, 26)), as a numeric offset from today (-20), or as a string of periods and units ('+1M +10D').

$('.datepicker').datepicker({
      changeMonth: true,
      changeYear: true,
      minDate:'-80Y',
      maxDate:'-1Y',
      showAnim:'clip'
    });

This works.

Post Status

Asked in February 2016
Viewed 1,429 times
Voted 8
Answered 1 times

Search




Leave an answer