Skip to content

Instantly share code, notes, and snippets.

@chevcast
Created March 18, 2024 15:29
Show Gist options
  • Save chevcast/864e70aa1a785cd71ef1beae145cfae4 to your computer and use it in GitHub Desktop.
Save chevcast/864e70aa1a785cd71ef1beae145cfae4 to your computer and use it in GitHub Desktop.
/**
* Calculates the date of Easter Sunday for a given year in the Gregorian calendar.
*
* The method used is the Anonymous Gregorian algorithm (also known as Computus),
* which calculates Easter as the first Sunday after the first ecclesiastical full moon
* that occurs on or after March 21.
*
* @param year The year for which to calculate Easter Sunday (e.g., 2024).
* @returns A Date object representing Easter Sunday for the given year.
*/
function calculateEaster(year: number): Date {
// The Golden Number - 1
const G = year % 19;
// Century number
const C = Math.floor(year / 100);
// Corrections for leap years and moon's orbit
const H = (C - Math.floor(C / 4) - Math.floor((8 * C + 13) / 25) + 19 * G + 15) % 30;
// Correction for Sunday
const I = H - Math.floor(H / 28) * (1 - Math.floor(H / 28) * Math.floor(29 / (H + 1)) * Math.floor((21 - G) / 11));
// Weekday for the full moon
const J = (year + Math.floor(year / 4) + I + 2 - C + Math.floor(C / 4)) % 7;
// Number of days from March 21 to the Paschal full moon
const L = I - J;
// Month of Easter
const month = 3 + Math.floor((L + 40) / 44);
// Day of Easter
const day = L + 28 - 31 * Math.floor(month / 4);
// Return the date of Easter
return new Date(year, month - 1, day);
}
// Example usage:
const easterDate = calculateEaster(2024);
console.log(`Easter 2024 falls on: ${easterDate.toDateString()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment