Link to home
Start Free TrialLog in
Avatar of tablaFreak
tablaFreakFlag for United States of America

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!


$(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']};

Open in new window

Avatar of Michael Vasilevsky
Michael Vasilevsky
Flag of United States of America image

Use the beforeShowDay function. It goes something like this:

const array = ["2018-11-22","2018-11-23","2018-12-25"]

$('.date-pick').datepicker({
    beforeShowDay: function(date) {
        var string = $.datepicker.formatDate('yy-mm-dd', date);
        return [ array.indexOf(string) == -1 ]
    };
});

Open in new window


jsfiddle
Avatar of tablaFreak

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);
    };

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of tablaFreak
tablaFreak
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial