tablaFreak
asked on
jquery datePicker - exclude holidays from defined array
Hello Experts -
I'm using the datePicker jquery plugin for a calendar dropdown date selection, and I want to exclude weekends and holidays. I've got the weekends disabled (see Javascript function below), but would like to disable holidays as well, as defined by the array in the code below. Could someone show me how to reference the Date.prototype.holidays array to exclude holidays from the datePicker calendar?
Thank you!
I'm using the datePicker jquery plugin for a calendar dropdown date selection, and I want to exclude weekends and holidays. I've got the weekends disabled (see Javascript function below), but would like to disable holidays as well, as defined by the array in the code below. Could someone show me how to reference the Date.prototype.holidays array to exclude holidays from the datePicker calendar?
Thank you!
$(document).ready(function () {
$(".date-pick").datepicker({ startDate: '01/01/2000', clickInput: true, beforeShowDay: $.datepicker.noWeekends });
});
Date.prototype.holidays = {all:['0101','0704','1225'],
2018:['0903','1122','1123','1226','1227','1228','1231'],
2019:['0527','0902','1128','1129','1226','1227','1230','1231'],
2020:['0525','0907','1126','1127','1228','1229','1230','1231'],
2021:['0531','0906','1125','1126','1227','1228','1229','1230','1231'],
2022:['0530','0905','1124','1125','1226','1227','1228','1229','1230'],
2023:['0529','0904','1123','1124','1226','1227','1228','1229']};
ASKER
Thanks, Michael - the problem is I want to disable weekends and holidays, and preferably use the array I sent in the question. I wound up finding my own solution, as such:
$(document).ready(function () {
$("#datepicker").datepicker({
beforeShowDay: function (dt) {
var date = new Date(dt);
return [!date.isHoliday()]
}
});
});
Date.prototype.holidays = {all:['0101','0704','1225'],
2018:['0903','1122','1123','1226','1227','1228','1231'],
2019:['0527','0902','1128','1129','1226','1227','1230','1231'],
2020:['0525','0907','1126','1127','1228','1229','1230','1231'],
2021:['0531','0906','1125','1126','1227','1228','1229','1230','1231'],
2022:['0530','0905','1124','1125','1226','1227','1228','1229','1230'],
2023:['0529','0904','1123','1124','1226','1227','1228','1229']};
Date.prototype.isHoliday = function () {
function zeroPad(n) {
n |= 0;
return (n < 10 ? '0' : '') + n;
}
// if weekend return true from here it self;
if (this.getDay() == 0 || this.getDay() == 6) {
return true;
}
var day = zeroPad(this.getMonth() + 1) + zeroPad(this.getDate());
// if date is present in the holiday list return true;
return !!~this.holidays.all.indexOf(day) ||
(this.holidays[this.getFullYear()] ?
!!~this.holidays[this.getFullYear()].indexOf(day) : false);
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
jsfiddle