Tuesday, February 28, 2012

Date of Easter in JavaScript

A couple of years ago, I posted source code in F# for calculating the date of Easter. Well, that time of year is here again, but this time around I'll post a JavaScript example.

As before, the method is from Butcher's Ecclesiastical Calendar (1876). I present it in the form given in Peter Duffett-Smith's Practical Astronomy With Your Calculator (Cambridge 1981, the 1988 version is still available from Amazon, et al). And once again, I'd like to add that Mr. Duffett-Smith's books are among the most fun and interesting math books you'll ever find!

I'm no JavaScript wiz, but I did test it in both MSIE7 and Chrome. However, as always, this code is presented “as-is” and without warranty or implied fitness; use at your own risk.

function easterForYear (year) {
var a = year % 19;
var b = Math.floor(year / 100);
var c = year % 100;
var d = Math.floor(b / 4);
var e = b % 4;
var f = Math.floor((b + 8) / 25);
var g = Math.floor((b - f + 1) / 3);
var h = (19 * a + b - d - g + 15) % 30;
var i = Math.floor(c / 4);
var k = c % 4;
var l = (32 + 2 * e + 2 * i - h - k) % 7;
var m = Math.floor((a + 11 * h + 22 * l) / 451);
var n0 = (h + l + 7 * m + 114)
var n = Math.floor(n0 / 31) - 1;
var p = n0 % 31 + 1;
var date = new Date(year,n,p);
return date;
}

2 comments:

John said...

I wonder how much effort it would take to convert the routine to calculate related dates, e.g. Shrove Tuesday, Whit Sunday, etc. Will have to try it some time...

TechNeilogy said...

First, thanks for commenting! Sounds like a fascinating idea. I have to admit I've never taken the time to fully understand the "Easter algorithm" itself. It forms a prelude to the book I mentioned, mainly to get one used to the notation, etc., so I've always reserved a full understanding for some rainy day...