UNPKG

easter-date

Version:

Calculate the date of Easter for a given year

54 lines (53 loc) 1.59 kB
/** * Returns the date of Easter for a given year. * Function is an alias for getEasterSunday() function * * @example Get Easter date for a given year * ```ts * import { getEaster } from "easter-date"; * const easter = getEaster(2023); * console.log(easter); // 2023-04-09T00:00 * ``` * @param year */ export function getEaster(year) { const a = year % 19; const b = Math.floor(year / 100); const c = year % 100; const d = Math.floor(b / 4); const e = b % 4; // eslint-disable-line unicorn/prevent-abbreviations const f = Math.floor((b + 8) / 25); const g = Math.floor((b - f + 1) / 3); const h = (19 * a + b - d - g + 15) % 30; const i = Math.floor(c / 4); const k = c % 4; const l = (32 + 2 * e + 2 * i - h - k) % 7; const m = Math.floor((a + 11 * h + 22 * l) / 451); const month = Math.floor((h + l - 7 * m + 114) / 31); const day = ((h + l - 7 * m + 114) % 31) + 1; return new Date(Date.UTC(year, month - 1, day)); } /** * Returns true if the given date is Easter Sunday. * @param date */ export function isEaster(date) { const easter = getEaster(date.getFullYear()); return (date.getFullYear() === easter.getFullYear() && date.getMonth() === easter.getMonth() && date.getDate() === easter.getDate()); } /** * Returns the date of Easter for a given year. * @param year */ export function getEasterSunday(year) { return getEaster(year); } /** * Returns true if the given date is Easter Sunday. * @param date */ export function isEasterSunday(date) { return isEaster(date); }