Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions chapters/dates_and_times/date-of-easter.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
layout: recipe
title: Calculate the date of Easter Sunday
chapter: Dates and Times
---

h2. Problem

You need to find the month and day of the Easter Sunday for given year.

h2. Solution

The following function returns array with two elements: month (1-12) and day of the Easter Sunday. If no arguments are given
result is for the current year.
This is an implementation of "Anonymous Gregorian algorithm":http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm in CoffeeScript

{% highlight coffeescript %}

gregorianEaster = (year = (new Date).getFullYear()) ->
a = year % 19
b = ~~(year / 100)
c = year % 100
d = ~~(b / 4)
e = b % 4
f = ~~((b + 8) / 25)
g = ~~((b - f + 1) / 3)
h = (19 * a + b - d - g + 15) % 30
i = ~~(c / 4)
k = c % 4
l = (32 + 2 * e + 2 * i - h - k) % 7
m = ~~((a + 11 * h + 22 * l) / 451)
n = h + l - 7 * m + 114
month = ~~(n / 31)
day = (n % 31) + 1
[month, day]

{% endhighlight %}

h2. Discussion

NB! Javascript numbers months from 0 to 11 so .getMonth() for date in March will return 2, this function will return 3.
You can modify the function if you want this to be consistent.

{% highlight coffeescript %}

gregorianEaster() # => [4, 24] (April 24th in 2011)
gregorianEaster 1972 # => [4, 2]

{% endhighlight %}

1 change: 0 additions & 1 deletion wanted-recipes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ evens.every even
h2. Dates and Times

* Calculating the phase of the moon
* Holidays: Calculating Easter
* Holidays: Calculating US Thanksgiving
* Holidays: Calculating CA Thanksgiving
* Number of days between two dates
Expand Down