Last active
January 10, 2023 05:40
-
-
Save Dodotree/1262bb33a6e554a220674e1ff5117ac2 to your computer and use it in GitHub Desktop.
Generates dates of federal holidays for the given year (USA)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function federalHolidaysYear(year){ | |
| /* Month counts from 0, week counts from Sunday 0, week day number in the month also from 0 */ | |
| /* "Last week day on the month" is the next month week day with num=-1 */ | |
| var holidays = [{name: "New Year's Day", fixed: true, mo: 0, date: 1}, | |
| {name: "Birthday of Martin Luther King, Jr.", fixed: false, mo: 0, weekDay: 1, num: 2}, | |
| {name: "Washington's Birthday", fixed: false, mo: 1, weekDay: 1, num: 2}, | |
| {name: "Memorial Day", fixed: false, mo: 5, weekDay: 1, num: -1}, | |
| {name: "Juneteenth National Independence Day", fixed: true, mo: 5, date: 19}, | |
| {name: "Independence Day", fixed: true, mo: 6, date: 4 }, | |
| {name: "Labor Day", fixed: false, mo: 8, weekDay: 1, num: 0}, | |
| {name: "Columbus Day", fixed: false, mo: 9, weekDay: 1, num: 1 }, | |
| {name: "Veterans Day", fixed: true, mo: 10, date: 11 }, | |
| {name: "Thanksgiving Day", fixed: false, mo: 10, weekDay: 4, num: 3 }, | |
| {name: "Christmas Day", fixed: true, mo: 11, date: 25 }]; | |
| var dates = []; | |
| holidays.forEach(h=>{ | |
| if (h.fixed){ | |
| dates.push( new Date(year, h.mo, h.date) ); | |
| }else { | |
| let dif = (7 + h.weekDay - new Date(year, h.mo, 0).getDay() ) % 7; | |
| dates.push( new Date(year, h.mo, dif + 7*h.num) ); | |
| } | |
| }); | |
| return dates; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment