Skip to content

Commit ecd8af1

Browse files
committed
Merge pull request coffeescript-cookbook#25 from imcat/date_of_easter
Date of easter
2 parents a7c0c38 + 712c078 commit ecd8af1

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
layout: recipe
3+
title: Calculate the date of Easter Sunday
4+
chapter: Dates and Times
5+
---
6+
7+
h2. Problem
8+
9+
You need to find the month and day of the Easter Sunday for given year.
10+
11+
h2. Solution
12+
13+
The following function returns array with two elements: month (1-12) and day of the Easter Sunday. If no arguments are given
14+
result is for the current year.
15+
This is an implementation of "Anonymous Gregorian algorithm":http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm in CoffeeScript
16+
17+
{% highlight coffeescript %}
18+
19+
gregorianEaster = (year = (new Date).getFullYear()) ->
20+
a = year % 19
21+
b = ~~(year / 100)
22+
c = year % 100
23+
d = ~~(b / 4)
24+
e = b % 4
25+
f = ~~((b + 8) / 25)
26+
g = ~~((b - f + 1) / 3)
27+
h = (19 * a + b - d - g + 15) % 30
28+
i = ~~(c / 4)
29+
k = c % 4
30+
l = (32 + 2 * e + 2 * i - h - k) % 7
31+
m = ~~((a + 11 * h + 22 * l) / 451)
32+
n = h + l - 7 * m + 114
33+
month = ~~(n / 31)
34+
day = (n % 31) + 1
35+
[month, day]
36+
37+
{% endhighlight %}
38+
39+
h2. Discussion
40+
41+
NB! Javascript numbers months from 0 to 11 so .getMonth() for date in March will return 2, this function will return 3.
42+
You can modify the function if you want this to be consistent.
43+
44+
{% highlight coffeescript %}
45+
46+
gregorianEaster() # => [4, 24] (April 24th in 2011)
47+
gregorianEaster 1972 # => [4, 2]
48+
49+
{% endhighlight %}
50+

wanted-recipes.textile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ evens.every even
4242
h2. Dates and Times
4343

4444
* Calculating the phase of the moon
45-
* Holidays: Calculating Easter
4645
* Holidays: Calculating US Thanksgiving
4746
* Holidays: Calculating CA Thanksgiving
4847
* Number of days between two dates

0 commit comments

Comments
 (0)