Reputation: 317
I've tried for hours with no luck. I need to first exclude all days except tuesday and friday. Then there's an array of certain Tuesdays and Fridays I need to disable. Alone each function works perfectly but for the life of me they will not work together?
working exclude all days except Tuesday and Friday:
$(function(){
$("#datepicker").datepicker(
{ beforeShowDay: function(day) {
var day = day.getDay();
var taken = ["2020-03-17"];
var isTaken = '2020-03-17';
if (day == 0 || day == 1 || day == 3 || day == 4 || day == 6) {
return [false]
} else {
return [true];
}
}
});
});
Working exclude selected dates:
var array = ["03/17/2020","03/18/2020","03/19/2020"];
$(function(){
$('#datepicker').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('mm/dd/yy', date);
return [ array.indexOf(string) == -1 ]
}
});
});
How can I get these to work both at the same time??
Upvotes: 0
Views: 406
Reputation: 14520
You were almost all the way there, you just have to combine your conditions. Working JSFiddle.
I made some other minor changes:
replaced jQuery
with $
for consistency;
renamed some variables for clarity (eg it is a date
passed to beforeShowDay
, not a day
, array
doesn't remind us what it is or what we need to do with it). It is easy to get things mixed up if you have 3 or 4 variables and all of them are dates or date-related;
Inverted your day of week test. Instead of testing which day to disallow, it is simpler to think of days allowed, especially when there are less of them;
Here's the code:
$(function(){
var day, formatted,
disallowed = ["03/17/2020","03/18/2020","03/19/2020"];
$("#datepicker").datepicker({
beforeShowDay: function(date) {
day = date.getDay();
formatted = $.datepicker.formatDate('mm/dd/yy', date);
// If it is a Tue or a Fri, AND it is not in the disallowed list,
// it should be selectable
if ((day == 2 || day == 5) && disallowed.indexOf(formatted) === -1) {
return [true];
} else {
return [false];
}
}
})
});
Upvotes: 1