Biomehanika
Biomehanika

Reputation: 1540

Disable mondays and saturdays on jQuery datepicker

I need jQuery datepicker to disable an array of specific dates as well as all mondays and all saturdays. I have archieved the first two goals, but not the third one (disable saturdays). Here's the code:

let fechasEspecificas = ["2018-11-06"]

jQuery.datepicker.setDefaults({
  "minDate": 2,
  "maxDate": new Date(2019,0,31),
  beforeShowDay: function(date) {
      let string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      if (contains(fechasEspecificas,string)) {
        return [false, '']  // here are specific dates disabled
      } else {
        let day = date.getDay();
        return [(day != 1), '']; // here are mondays disabled
      }
    }
});


function contains(a, obj) {var i = a.length;while (i--) {if (a[i] === obj){return true;}}return false;}

JSFIDDLE demo

How could be the code extended in order to disable also saturdays?

Upvotes: 0

Views: 944

Answers (2)

Brank Victoria
Brank Victoria

Reputation: 1455

You must concatenate the days that you will disable with AND operation

let fechasEspecificas = ["2018-11-06"]

jQuery.datepicker.setDefaults({
    "minDate": 2,
  "maxDate": new Date(2019,0,31),
    beforeShowDay: function(date) {
      let string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      if (contains(fechasEspecificas,string)) {
        return [false, '']
      } else {
        let day = date.getDay();
        return [(day != 1) && (day != 6), '']; //Add the day number 6 for disable saturdays as well
      }
    }
});


function contains(a, obj) {var i = a.length;while (i--) {if (a[i] === obj){return true;}}return false;}


jQuery('input').datepicker();

Upvotes: 2

user9019817
user9019817

Reputation:

You should be able to change return [(day != 1), '']; to return [(day != 1 && day != 6), ''];

Full Code:

let fechasEspecificas = ["2018-11-06"]

jQuery.datepicker.setDefaults({
    "minDate": 2,
  "maxDate": new Date(2019,0,31),
    beforeShowDay: function(date) {
      let string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      if (contains(fechasEspecificas,string)) {
        return [false, '']
      } else {
        let day = date.getDay();
        return [(day != 1 && day != 6), ''];
      }
    }
});


function contains(a, obj) {var i = a.length;while (i--) {if (a[i] === obj){return true;}}return false;}


jQuery('input').datepicker();

That will disable Monday and Saturdays.

Upvotes: 1

Related Questions