Wednesday, July 30, 2008

When the heck is Easter?

Thanks to some arcane historical, and religious reasons, Easter has a tendency to slide around the calendar like a drunk man on an ice-rink. Fortunately it is still possible to calculate when it occurs in Flash with the following function:

function calcEaster(year) {
Months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
month = Math.floor((((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7 - 7 * Math.floor(((year % 19) + 11 * ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + 22 * (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7) / 451) + 114) / 31) - 1;
month = Months[month];
day = ((((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year %
19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7 - 7 * Math.floor(((year % 19) + 11 * ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + 22 * (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7) / 451) + 114) % 31) + 1;
}


The following code will calculate the date of Easter between the years 1950 and 2150. It uses the getOrdinalNumber function discussed elsewhere on this blog:

for (i = 1950; i <= 2150; i++) {
calcEaster(i);
trace("Easter falls on Sunday " + getOrdinalNumber(day) + " of " + month + ", " + i);
}

No comments: