Azira Haslin
Azira Haslin

Reputation: 17

Enable date every Thursday but disable certain Thursday

I would like to ask about datepicker. I am currently developing a website where university students have to pick their own Thursday to hear a talk about career.

First of all, I have managed to disable all days except Thursday. The code I wrote stated below :

<script>
  $( function() 
{
$( "#datepicker" ).datepicker({dateFormat: 'yy-mm-dd', minDate: 0, maxDate: 
'2018-12-15', beforeShowDay: function(date){var day = date.getDay();return 
[day == 4,'disable'];
}});
  } );
</script>

But now I would to disable a certain Thursday for example next Thursday which is: 'yy-mm-dd' = '2017-08-17'

Can someone explain to me how or where should I do the modification to the code which can allow me to disable a certain Thursday?

Refer this image for my datepicker https://drive.google.com/file/d/0ByJlnNeKIynkQ3IzRTN2cHg4WFE/view?usp=sharing

Upvotes: 0

Views: 73

Answers (2)

Kashif Solangi
Kashif Solangi

Reputation: 928

you can still use the onBeforeShowDay, since it will get called before the datepicker is displayed, because changing months will make the datepicker to render again.

You can use an array that stores the list of dates and change this based on the result from your ajax call. e.g:

//at first only september dates will be disabled.
var array = ["2017-08-10","2017-08-17","2017-08-24"];

 $('input').datepicker({
      onChangeMonthYear: function(year, month, inst) {
       // Perform AJAX call and get the list
       //override the array, and now the october dates will be disabled.
        $.ajax({
           url: ajaxUrl,
           type: "post",
           data: serializedData,
           async: false,
           success: function (data) {
             array = data; //["2017-08-10","2017-08-17","2017-08-24"];
          }
        });  
      },
     beforeShowDay: function (date) {
       var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
       return [array.indexOf(string) == -1 ]
     }
   });

Upvotes: 1

Chandra Kumar
Chandra Kumar

Reputation: 4205

Use like this:

Date.getDay() returns a value in the range 0-6, not 1-7.

beforeShowDay: function(date) {
    return [date.getDay() === 0,''];
}

Upvotes: 1

Related Questions