Created
December 14, 2021 13:41
-
-
Save roelvan/d5e5fd5cbb5a8311810327891f8a7b0c to your computer and use it in GitHub Desktop.
π§πͺ Checks if a date is a Belgian holiday
This file contains 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
// π§πͺ Checks if a date is a Belgian holiday | |
/* | |
The code below is not fully mine. | |
Everything is based on some snippets I found online (another github gist and something on stackoverflow if I am not mistaken). | |
I was in a hurry and forgot to keep track of my sources, my apologies. | |
*/ | |
// I did not bother translating all holiday names to English, was a bit easier to work with | |
// you could replace this with another date library or use native JS if bundle size is an issue | |
import { addDays, getYear, isSameDay } from 'date-fns'; | |
export const getNewYearsDay = (year) => new Date(year, 0, 1); | |
export const getLaborDay = (year) => new Date(year, 4, 1); | |
export const getNationaleFeestdag = (year) => new Date(year, 6, 21); | |
export const getOlvHemelvaart = (year) => new Date(year, 7, 15); | |
export const getAllerheiligen = (year) => new Date(year, 10, 1); | |
export const getWapenstilstand = (year) => new Date(year, 10, 11); | |
export const getChristmas = (year) => new Date(year, 11, 25); | |
export function getEaster(year) { | |
if (year < 325) { | |
throw new RangeError('Cannot calculate Easter dates before 325 AD.'); | |
} | |
function mod(a, b) { | |
return a % b; | |
} | |
function div(a, b) { | |
const q = a / b; | |
if (q < 0) { | |
throw new Error('Unexpected negative q'); | |
} | |
return Math.floor(q); | |
} | |
const y = year, | |
skipMarchDays = 21, | |
a = mod(y, 19), | |
b = div(y, 100), | |
c = mod(y, 100), | |
d = div(b, 4), | |
e = mod(b, 4), | |
f = div(b + 8, 25), | |
g = div(b - f + 1, 3), | |
h = mod(19 * a + b - d - g + 15, 30), | |
i = div(c, 4), | |
k = mod(c, 4), | |
l = mod(32 + 2 * e + 2 * i - h - k, 7), | |
m = div(a + 11 * h + 22 * l, 451), | |
t = h + l - 7 * m + skipMarchDays, | |
n = div(t, 31) + 3, | |
p = mod(t, 31); | |
return new Date(year, n - 1, p + 1); | |
} | |
// 1 day after easter | |
export const getEasterMonday = (year) => addDays(getEaster(year), 1); | |
// 39 days after easter | |
export const getOlhHemelvaart = (year) => addDays(getEaster(year), 39); | |
export const getPinksteren = (year) => addDays(getOlhHemelvaart(year), 10); | |
export const getPinksterMaandag = (year) => addDays(getPinksteren(year), 1); | |
const getHolidays = (year) => ({ | |
newYearsDay: getNewYearsDay(year), | |
paasmaandag: getEasterMonday(year), | |
laborDay: getLaborDay(year), | |
olhHemelvaart: getOlhHemelvaart(year), | |
pinkstermaandag: getPinksterMaandag(year), | |
nationaleFeestdag: getNationaleFeestdag(year), | |
olvHemelvaart: getOlvHemelvaart(year), | |
allerheiligen: getAllerheiligen(year), | |
wapenstilstand: getWapenstilstand(year), | |
christmas: getChristmas(year), | |
}); | |
const isInHolidayList = (date) => { | |
const holidays = getHolidays(getYear(date)); | |
let found = false; | |
for (const holidayName of Object.keys(holidays)) { | |
if (isSameDay(date, holidays[holidayName])) { | |
found = true; | |
break; // exit the loop as soon as we found 1 match | |
} | |
} | |
return found; | |
}; | |
// export const getHolidaysFormatted = (year = new Date().getFullYear()) => { | |
// const holidays = getHolidays(year); | |
// return Object.keys(holidays).map((holidayName) => { | |
// return format(holidays[holidayName], 'dd/MM/yyyy'); | |
// }); | |
// }; | |
export const isHoliday = (date) => isInHolidayList(date); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment