Skip to content

Instantly share code, notes, and snippets.

@johndyer
Last active April 9, 2024 16:28
Show Gist options
  • Select an option

  • Save johndyer/0dffbdd98c2046f41180c051f378f343 to your computer and use it in GitHub Desktop.

Select an option

Save johndyer/0dffbdd98c2046f41180c051f378f343 to your computer and use it in GitHub Desktop.
Calculate Easter in JavaScript
/**
* Calculates Easter in the Gregorian/Western (Catholic and Protestant) calendar
* based on the algorithm by Oudin (1940) from http://www.tondering.dk/claus/cal/easter.php
* @returns {array} [int month, int day]
*/
function getEaster(year) {
var f = Math.floor,
// Golden Number - 1
G = year % 19,
C = f(year / 100),
// related to Epact
H = (C - f(C / 4) - f((8 * C + 13)/25) + 19 * G + 15) % 30,
// number of days from 21 March to the Paschal full moon
I = H - f(H/28) * (1 - f(29/(H + 1)) * f((21-G)/11)),
// weekday for the Paschal full moon
J = (year + f(year / 4) + I + 2 - C + f(C / 4)) % 7,
// number of days from 21 March to the Sunday on or before the Paschal full moon
L = I - J,
month = 3 + f((L + 40)/44),
day = L + 28 - 31 * f(month / 4);
return [month,day];
}
@DHFW

DHFW commented May 29, 2019

Copy link
Copy Markdown

Thank you!

For others: be aware that this returns the month as 1 = January, 2 = February and so on.

I subtracted the month with one to be able to convert it to a Javascript Date easily in which the month is index based (0 = January, 1 = February).

@varvino

varvino commented Feb 6, 2020

Copy link
Copy Markdown

Thanks a lot for this

@gavinr

gavinr commented Mar 10, 2020

Copy link
Copy Markdown

I am using this here: Easter Dates
Thanks!

@johndyer

johndyer commented Mar 10, 2020 via email

Copy link
Copy Markdown
Author

@sunsero

sunsero commented Apr 11, 2020

Copy link
Copy Markdown

Is it just me or is the function returning Easter Monday this year? Happy Easter, anyway!

@johndyer

johndyer commented Apr 11, 2020 via email

Copy link
Copy Markdown
Author

@sunsero

sunsero commented Apr 11, 2020

Copy link
Copy Markdown

Don't think so because Easter Sunday is calculated for 2019 and for 2021 correctly. If it matters I'm during summer time in UTC+2, in winter in UTC+1.

@johndyer

Copy link
Copy Markdown
Author

getEaster(2020);
returns
[4, 12]
for me...

@sunsero

sunsero commented Apr 11, 2020

Copy link
Copy Markdown

Thanks, you're right - if I pass 2020 as a constant value rather than in a variable it gives the correct result. I'll have to figure out what's going on.

@Termplexed

Copy link
Copy Markdown

Minor nuisance. On line 12 there is thin-space in stead of normal space. (0x2009 instead of 0x20) in C - f

		H = (C<0x2009>-<0x2009>f(C / 4) - f((8 * C + 13)/25) + 19 * G + 15) % 30,

@johndyer

johndyer commented Mar 1, 2021

Copy link
Copy Markdown
Author

Thanks @Termplexed! Fixed.

@creat73

creat73 commented Apr 1, 2021

Copy link
Copy Markdown

This algorithm seems to fail for year 2022 returning 23.04 instead of 17.04. Works fine for any other year I tried. Any idea on why that might be? I'm really curious and couldn't find explanation.

@wysal

wysal commented Jun 9, 2021

Copy link
Copy Markdown

I needed to count days up from easter for public holidays calculation so I did some adaptions:

function getEaster(year, plusdays) {
var f = Math.floor,
G = year % 19,
C = f(year / 100),
H = (C - f(C / 4) - f((8 * C + 13) / 25) + 19 * G + 15) % 30,
I = H - f(H / 28) * (1 - f(29 / (H + 1)) * f((21 - G) / 11)),
J = (year + f(year / 4) + I + 2 - C + f(C / 4)) % 7,
L = I - J,
month = 3 + f((L + 40) / 44),
day = L + 28 - 31 * f(month / 4);
return new Date(year, month - 1, day + plusdays);
}

so this would then output for example Easter Monday:
getEaster(2022, 1)

in case of need for someone

@tdiluzio

Copy link
Copy Markdown

Thanks @johndyer and @wysal 👍

@P06316

P06316 commented Nov 26, 2021

Copy link
Copy Markdown

This algorithm seems to fail for year 2022 returning 23.04 instead of 17.04. Works fine for any other year I tried. Any idea on why that might be? I'm really curious and couldn't find explanation.

I had the same issue and solved it by applying parseInt(myYear, 0) on the variable containing the year.

console.log(getEaster("2022")); //Returns [4, 23] => NOK
console.log(getEaster(2022)); //Returns [4, 17] => OK
console.log(getEaster(parseInt("2022", 0))); //Returns [4, 17] => OK

@PenkoMlakar

Copy link
Copy Markdown

Thanks @johndyer!

@TheDoomfire

Copy link
Copy Markdown

Thanks seem to work!

Was wondering if it's possible to get the good Friday from this? Since it's the Friday before Easter.

So I guess day - 2 will do for most cases at least.

@CraigDavison

Copy link
Copy Markdown

When using this, I was passing the year in from an form input and it seems as though, only odd numbered years, returned the correct date (being Easter Sunday). The answer was to ensure the value passed in was an integer so added a line at the top of the function.

year = parseInt(year)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment