-3

I want to disable the 2nd Saturday, 4th Saturday, Sunday and public holidays, throughout the year, using jQuery Calendar.

Drenmi
  • 8,492
  • 4
  • 42
  • 51

2 Answers2

0

Jquery Calendar plugin provide you a option "beforeShowDay", you can find more information on DataPickerUI

To Disable 2nd Saturday & 4th Saturday, you need to first calculate the sat or sunday of specific month then disable those dates, like we did for others calendars

sample code calculate sat & sunday https://www.hscripts.com/scripts/JavaScript/second-fourth.php

Created plunker for you, https://plnkr.co/edit/inBYY748BptaCd7Ulwwg?p=preview

                //To disable Sundays you need to find out the Day of current date.
            $(function () {
                var publicHolidays = [
                  [11, 28, 2015],
                  [11, 30, 2015]
                ];

                $("#datepicker").datepicker({
                    beforeShowDay: function (date) {
                        var day = date.getDay();
                        return [(day !== 0), ''];
                    }
                });

                //To disable public holidays create an array with you holiday list then
                //return false when you browse calender.

                $("#datepicker2").datepicker({
                    beforeShowDay: function (date) {
                        for (i = 0; i < publicHolidays.length; i++) {
                            if (date.getMonth() == publicHolidays[i][0] &&
                              date.getDate() == publicHolidays[i][1] &&
                              date.getFullYear() == publicHolidays[i][2]) {
                                return [false];
                            }
                        }
                        return [true];
                    }
                });
            });
Arun Yadav
  • 21
  • 4
0

For what it's worth, here's a couple of functions for the second/fourth Saturday part of the problem.

Both functions accept an instance of javascript Date() and return true or false. You can use either one.

function is2ndOr4thSat_1(date) {
    var day = date.getDay(),
        week = Math.floor(date.getDate() / 7);
    return day == 6 && (week == 1 || week == 3)
}

Hopefully is2ndOr4thSat_1() is self explanatory.

function is2ndOr4thSat_2(date) {
    var d = date.getDate(),
        offset = (((1 + date.getDay() - d) % 7) + 7) % 7;
    return !((offset + d) % 14);
}

is2ndOr4thSat_2() is more obscure.

The expression (((1 + date.getDay() - d) % 7) + 7) % 7 finds the offset of the first of the month from a nominal zero (the 1st's preceding Saturday), using a true modulo algorithm that caters for negative numbers.

Then (offset + d) % 14 returns 0 if date is either 14 or 28 days ahead of the nominal zero, and ! converts to a boolean of the required sense (true for qualifying Saturdays otherwise false).

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44