Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting min date in jQuery datepicker

Hi I want to set min date in my jQuery datepicker to (1999-10-25). So I tried the below code it's not working.

$(function () {     $('#datepicker').datepicker({         dateFormat: 'yy-mm-dd',         showButtonPanel: true,         changeMonth: true,         changeYear: true,         showOn: "button",         buttonImage: "images/calendar.gif",         buttonImageOnly: true,         minDate: new Date(1999, 10 - 1, 25),         maxDate: '+30Y',         inline: true     }); }); 

If I change the min year to above 2002 than it will work fine but if I specify min year less than 2002 (like above example 1999), it will show only up to 2002.
I am using jquery-1.7.1.min.js and jquery-ui-1.8.18.custom.min.js.

like image 960
user1184777 Avatar asked Apr 13 '12 10:04

user1184777


People also ask

What is min date in datepicker?

The jQuery UI Datepicker minDate option is used to set the minimum selectable date. If we set it to null, there is no minimum date. Syntax: $( ". selector" ).

How do you set your minDate on datepicker?

To set it after init, use $('#datepicker'). datepicker( "option", "minDate", new Date( 1999, 10 - 1, 25 ) ) .


2 Answers

$(function () {     $('#datepicker').datepicker({         dateFormat: 'yy-mm-dd',         showButtonPanel: true,         changeMonth: true,         changeYear: true, yearRange: '1999:2012',         showOn: "button",         buttonImage: "images/calendar.gif",         buttonImageOnly: true,         minDate: new Date(1999, 10 - 1, 25),         maxDate: '+30Y',         inline: true     }); }); 

Just added year range option. It should solve the problem

like image 90
Gaurav Avatar answered Sep 19 '22 11:09

Gaurav


The problem is that the default option of "yearRange" is 10 years.

So 2012 - 10 = 2002.

So change the yearRange to c-20:c or just 1999 (yearRange: '1999:c'), and use that in combination with restrict dates (mindate, maxdate).

For more info: https://jqueryui.com/demos/datepicker/#option-yearRange


See example: (JSFiddle)

And your code with the addition:

$(function () {     $('#datepicker').datepicker({         dateFormat: 'yy-mm-dd',         showButtonPanel: true,         changeMonth: true,         changeYear: true,         showOn: "button",         buttonImage: "images/calendar.gif",         buttonImageOnly: true,         minDate: new Date(1999, 10 - 1, 25),         maxDate: '+30Y',         yearRange: '1999:c',         inline: true     }); }); 
like image 24
Marco Johannesen Avatar answered Sep 23 '22 11:09

Marco Johannesen