Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing minDate and maxDate on the fly using jQuery DatePicker

I'm having a particular issue with the jQuery Datepicker. I can easily add a date range, but I want the selectable range to change depending on the event the user has picked.

So if they pick event #1, they can only pick dates from event #1's date range.

I've written a simple little function that gets called whenever the user selects a new event, but it only ever shows the initially set minDate/maxDate values.

function datepicker(startDate, endDate) {   $( "#date" ).datepicker({     inline: true,     minDate: new Date(startDate),     maxDate: new Date(endDate),     dateFormat: 'dd/mm/yy'    }); } 

I've tried calling $('#date').datepicker('remove'); before calling the above function again, to see if it creates a new instance with the correct dates, but it doesn't seem to work.

I've checked all the data through developer tools and everything is being called and passed correctly. Is there anything I can do to make this work how I want it to?

like image 726
Chuck Le Butt Avatar asked Apr 28 '13 21:04

Chuck Le Butt


People also ask

What is minDate and maxDate in jQuery datepicker?

If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.

How do I remove my minDate on datepicker?

Try this $( "#MyDatepicker" ). datepicker( "option", "minDate", null) .

How do I maxDate datepicker?

Try this: $("#datepicker"). datepicker({ minDate: -0, maxDate: new Date(2013, 1, 18) }); If you want use hard coded date, use the new Date(2013, 1, 18) pattern.

How do I change the language on datepicker?

Edit file "bootstrap-datepicker.js" to add your languages: Show activity on this post. Download Language wise all datepicker files from here: [https://github.com/jquery/jquery-ui/tree/master/ui/i18n][1] I hope it will work.


1 Answers

You have a couple of options...

1) You need to call the destroy() method not remove() so...

$('#date').datepicker('destroy'); 

Then call your method to recreate the datepicker object.

2) You can update the property of the existing object via

$('#date').datepicker('option', 'minDate', new Date(startDate)); $('#date').datepicker('option', 'maxDate', new Date(endDate)); 

or...

$('#date').datepicker('option', { minDate: new Date(startDate),                                   maxDate: new Date(endDate) }); 
like image 188
Jared Avatar answered Sep 21 '22 11:09

Jared