22

I am trying to disable specific dates using the JQuery Ui. However, I am having no luck, here is my code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <link rel="stylesheet" type="text/css" href="development-bundle/themes/ui-lightness/jquery.ui.all.css"> <style type="text/css"> .ui-datepicker .preBooked_class { background:#111111; } .ui-datepicker .preBooked_class span { color:#999999; } </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>jQuery UI Datepicker</title> <script type="text/javascript" src="development-bundle/jquery-1.7.1.js"></script> <script type="text/javascript" src="development-bundle/ui/jquery.ui.core.js"></script> <script type="text/javascript" src="development-bundle/ui/jquery.ui.widget.js"></script> <script type="text/javascript" src="development-bundle/ui/jquery.ui.datepicker.js"></script> 

Instantiate datepicker object

<script type="text/javascript"> $(function() { $( "#iDate" ).datepicker({ dateFormat: 'dd MM yy', beforeShowDay: checkAvailability }); }) 

Get the dates to be disabled within the calendar

 var unavailableDates = ["9-3-2012","14-3-2012","15-3-2012"]; function unavailable(date) { dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear(); if ($.inArray(dmy, unavailableDates) == -1) { return [true, ""]; } else { return [false,"","Unavailable"]; } } $('#iDate').datepicker({ beforeShowDay: unavailable }); </script> </head> <body> <input id="iDate"> </body> </html> 

It doesn't seem to be working, any idea how I can resolve this. cheers.

4 Answers 4

43

It looks like you're calling datepicker twice on one input. Its kind of hard to follow your code, but if you re-organize it and remove the second datepicker call, everything should work:

<script type="text/javascript"> var unavailableDates = ["9-3-2012", "14-3-2012", "15-3-2012"]; function unavailable(date) { dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear(); if ($.inArray(dmy, unavailableDates) == -1) { return [true, ""]; } else { return [false, "", "Unavailable"]; } } $(function() { $("#iDate").datepicker({ dateFormat: 'dd MM yy', beforeShowDay: unavailable }); }); </script> 

Check this article: disable-dates-in-datepicker

Sign up to request clarification or add additional context in comments.

5 Comments

Works perfectly, a quick question andrew. How can I adapt this so that instead of the unavailable dates being hard coded within the array, the dates are retrieved from a database table. cheers.
@bobo2000: That depends on your server-side technology. But I would imagine you would embed the array of excluded dates on the page on page load instead of hard coding them in the JavaScript.
Using PHP. Will I need to query the db then populate the array with the result set?
@bobo2000: That sounds correct. I'm not very knowledgeable with PHP though. Sounds like a candidate for a new question :)
This worked but how can I do it without specifying the dates in the array , it say in need the next three days . Like if it comes to tomorrow it selects the following three like that
0

Helpful answer..If you want to disable a perticular day, you can do as below:

 $scope.dateOptions = { beforeShowDay: unavailable }; function unavailable(date) { if (date.getDay() === 0) { return [true, ""]; } else { return [false, "", "Unavailable"]; } } 

the above will only enable Sunday and all other days will be disabled. Hope this helps.

Comments

0
var unavailableDates = ["23-09-2022", "24-09-2022", "25-09-2022"]; $('#res_date').datepicker({ startDate: '-0m', // this for disable past days format: 'dd/mm/yyyy', todayHighlight:'TRUE', autoclose: true, datesDisabled: unavailableDates // for disable your specific days }); 

Comments

0

To flip the answer on its head for available dates you can now use the javascript includes() and return the bool value in an array.

var availableDates = ["5-6-2023", "6-6-2023", "7-6-2023"]; function available(date) { dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear(); return [availableDates.includes(dmy)]; } $("#iDate").datepicker({ dateFormat: 'dd MM yy', beforeShowDay: available }); 

The 2nd element in the array is a css class and the 3rd param is an optional tooltip (https://api.jqueryui.com/datepicker/#option-beforeShowDay)

Worth saying that as of 2023 Jquery UI is on the way out, but its still quite a nice toolset.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.