File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change @@ -42,7 +42,6 @@ evens.every even
42
42
h2. Dates and Times
43
43
44
44
* Calculating the phase of the moon
45
- * Holidays: Calculating Easter
46
45
* Holidays: Calculating US Thanksgiving
47
46
* Holidays: Calculating CA Thanksgiving
48
47
* Number of days between two dates
You can’t perform that action at this time.
0 commit comments