Friday, May 6, 2011

jQuery - Datepicker - Disable Specific Dates

You can disable specific dates with the jQuery Datepicker using the beforeShowDay event.

var unavailableDates = ["9-5-2011","14-5-2011","15-5-2011"];

function unavailable(date) {
  dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
  if ($.inArray(dmy, unavailableDates) < 0) {
    return [true
,"","Book Now"];
  } else {
    return [false
,"","Booked Out"];

$('#iDate').datepicker({ beforeShowDay: unavailable });

Note: iDate in the last line is the name of the HTML form input element.

To see a working example with some more features please see the jsFiddle example: HERE
Anonymous said...

Cool - that worked for me... Thanks!

Anonymous said...

its awsummmmmmmm wonderful thank you so much you solved my problem.

Anonymous said...

thank you :)

Anonymous said...

I works perfectly, thanks. Would you know how to load the dates of the array dynamically, from a database?

Tokes said...

First you would need to make an SJAX call to a PHP file that would have something like this:

$dates = array();
$sqldata = mysql_query("SELECT `date` FROM `date_table`);
while($date = mysql_fetch_assoc($sqldata)) {
$dates[] = $date;
echo json_encode($dates);

Then on the JavaScript side:

unavailableDates = new Array();
unavailableDates = eval("("+result+")");

result being the returned data from the SJAX call see:

Anonymous said...

Nice one.. Was wondering how to and did not noticed this option in the documentation.. So thanks :)

Anonymous said...

thanks very useful

Mukesh B. Rane said...

hey thank you very much it's really nice

Jenny said...

Would you be able to show a jsfiddle so that all dates are disabled except for ones in the array?

Tokes said...

Hi Jenny,

Simply swap the IF ELSE statement.

EMG said...

It is possible to get a working example for the people that doesn't know to integrate it in a webpage??? (the me...) Tks....

Anonymous said...

just what I was looking for, thanks!

Anonymous said...

Its working.
But not working with dates
var unavailableDates = ["09-05-2011","14-05-2011","15-05-2011"];

Jim said...

Great post! :D

I have looked for something like this for a looong time! :D

Greg said...

Hi, thanks a lot for that, it's working perfect except one thing, when I use ajax to get the unavailability from the database, i get a javascript error:

ba is undefined
Line 47

if I remove the ajax statement and use the basic function I dont get the error. I compared the array return from the db to the one in the example and they look exactly the same. here is my js function:
base_url+"booking/getDates", {house: house, room: room},
unavailableDates = new Array();
unavailableDates = data;
dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
console.log("unavailableDates: ", unavailableDates);
if ($.inArray(dmy, unavailableDates) == -1) {
return [true, ""];
} else {
return [false,"","Unavailable"];

Thank you in advance

Jorge Navas Alejo said...


Amazing job dude. Just one question:

Is it possible to combine it with more clauses? In my cause, I'd like to disable specific dates AND weekends.

Thanks a lot!

Tokes said...

Please see more advanced example here
I have allowed disabling any days you want, the example shows weekends.

Anonymous said...

Fantastic example. I was wondering how you would disable specific dates after the datepicker was initially configured with beforeshowdates?

Craig Constable said...

You can remove the date picker using:

You can then update the unavailableDates array and re-add the datepicker using:
$('#iDate').datepicker({ beforeShowDay: unavailable });

Anonymous said...

Thanks Craig. After two days of searching around I'm thinking you're right about that. Was hoping that there was a way to update the element without destroying it.

Craig Constable said...

If you are simply changing the unavailableDates array you could just call: $('#iDate').datepicker("refresh");

Anonymous said...

It doesn't work.

Craig Constable said...

What issue are you having? You can see a working example in the jsFiddle link.

Anonymous said...

It's very usefull, thanx...

Anonymous said...

thnax for posting solved mine problem too

Anonymous said...

but can tell me how to overwrite a class of "td" element ?

Anonymous said...

Hola, soy novato en esto y necesito las bondades de esta herramienta. Me podrían decir cómo inserto el código en mi sitio. Tengo instalado el datepicker 1.7.2.min.js y 1.8.22.custom.min.js, con el tema le-frog; al cual lo pasé al español y con el formato La pregunta sería dónde tendría que insertar el código js y si le tengo que hacer alguna modificación respecto mi instalación y dónde tendría que insertar las nuevas líneas css. He intentado por horas varias cosas y no logro que funcione. Espero vuestra ayuda y gracias!!!!!!

Craig Constable said...


Probablemente la mejor manera de insertar este código en la parte inferior de su página en una etiqueta SCRIPT. cambiar a es fácil basta con cambiar la línea:
dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
dmy = date.getDate() + "." + (date.getMonth()+1) + "." + date.getFullYear();

si usted necesita un 0 a la izquierda se puede ver el ejemplo que hice aquí: se añade un 0 a la salida y lleva a los dos últimos dígitos.

Si quiere añadir css se puede ir ya sea en un archivo externo o en la propia página.

Espero que esto ayude!

Anonymous said...

Thank you for this helpful code.


Anonymous said...

Great option, helped a lot

Anonymous said...

It's stop working when date format is [d-m-Y];
and only work for [j-n-Y];
see this link
help me for [d-m-y] format.


Anonymous said...

it's work well for "9-5-2011" but not work for "09-05-2011".
help me guys..

Craig Constable said...

Checkout the jsFiddle I linked in the post

Anonymous said...

Perfect answer.
:) :)


Tapwell Bathrooms said...

Perfect and simple for our needs.

Is there any way to use 'minDate: +x' but only include days that are available? e.g. If I set only weekdays to available, and I set minDate to 2, and the user views the calendar on a Friday, Monday will be unavailable.

Craig Constable said...

Hi Tapwell Bathrooms,

Can probably be simplified but here is a working sample:

Dan / Tapwell said...

Amazing, much appreciated!

stevo said...

For days whose date is less than ten you need the following instead:

function unavailable_b(date) {
var dmy = ("0" + date.getDate()).slice(-2) + "-" + ("0" + (date.getMonth()+1)).slice(-2) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) < 0) {
return [true,"","Book Now"];
} else {
return [false,"","Booked Out"];

Craig Constable said...

Hi Stevo,

Yes that is in the advanced example in the notes.
And no it is only needed when using dates with a preceding zero.

Heather Kelly said...

Possible to block out the two weeks from the date of purchase? So if the order date is today, can the next two weeks be grated out?

Craig Constable said...

Hi Heather,
Yes. If you have the order date recorded in a database you can simply retrieve that and use it in a javascript function that creates an array of dates starting with that date until the array has a count of 14 dates.
$result = mysql_query("SELECT max(`order_date`) FROM `orders`");
$order_date = mysql_result($result, 0);
echo "[script]order_date = '" . $order_date . "';[/script]

order_date = new Date(order_date);
unavailableDates = new Array();
while (unavailableDates.length < 14) {
tempdate = new Date();
Note: Not tested but should work or at least give you the idea of how to accomplish this task. Also script tags were changed from <> to []

Heather Kelly said...

Thanks for your help Craig - a big beyond my capabilities I think...i need to hire a programmer!!

Heather Kelly said...

Can anyone help with the coding for greying out 12 weeks from the date of the order? I've re-read this forum but still haven't been able to figure it out. so if someone orders today, I want to grey out the dates between today and 12 weeks from then. Thanks a mil.

Heather Kelly said...

At the very least I need to make the field required. Thanks!

Craig Constable said...

Hi Heather,

Baby Giz said...

Wow!! Thank you!

Rukeshreddy PT said...

How to download javascript files from this jsfiddle...Please help me

Liz OXYwater said...

How do you turn off the following 7 days after today's date? from the delivery date picker?

Anonymous said...

Hi! In New Zealand we always put the date first, then the month, then year....
At the moment it's showing month -date- year... How can I change that?

Craig Constable said...

This is also using day-month-year so I'm not sure what you mean.

Marcie R. said...

Excellent! I've added this to my Shopify store with your terrific instructions but can't figure out where to add code in the snippet to remove Sundays and holidays. Can you please assist? Thank you!

andrea hernandez said...


I would like to know how to x unavailable dates out or highlight just the days that are available. I only deliver on weekends so I would love to know how to be able to make it easier to visualize that.

I love this, it was very helpful I was just wondering if the above can be done. Any help or information helps.


Craig Constable said...

Hi Andrea, Yes. Please look at the example you could enter Monday to Friday in the unavailable days.

Maxime D. said...

Hi Craig could you help with our website pleaseeee

Maxime D. said...

where can i send you the current code that I have

Maxime D. said...

i just don t understand how to add the code that you have to our code no matter how I put it it won t work?

Melissa said...

I want to thank you for your support!
I'm trying to disable dates from a SQL Server table and trying to understand the example that you gave to Heather.
I have a system in Classic ASP, with a table with holidays and special dates on a SQL Server table (id and date), and want to disable this dates.
Could you help me please?

Craig Constable said...

Hi Melissa,
You need to retrieve the list of dates from your SQL Server table using ASP and then place that information into javascript.

I don't use ASP but you need to run something like this:

SELECT FORMAT(dateField,'D-M-YYYY') AS unavailableDates FROM holidaysTable;

You then need to get each of the dates into javascript so it looks like:

var unavailableDates = ["9-5-2011","14-5-2011","15-5-2011"];

Anonymous said...

Hi there.
I'm a newbie at this so any help or direction would be amazing. I am using the following code in Shopify and would like to make some edits to the calendar. I would like to set Saturday as off- and be able to specify other days as off. I have tried to edit the code based on other responses above but seem to be having some issues. Here is the code that I am working with:

jQuery(function() {
minDate: +1,
maxDate: "+2M"
} );

Craig Constable said...

Did you check out the working example with more features mentioned in the post? You can use that and just remove Sunday from the unavailable days array.

Girl with Kaleidescope Eyes said...

Hi JQuery,

How do you apply this code into shopify? I'm having trouble applying this on shopify. Thank you

Post a Comment