Sunday, August 16, 2009

Easter revisited

Just changed the previous function to calculate Easter Day into a Date prototype. The prototype automatically returns a new Date() object for Easter for the current year but it will also accept an argument for any year:


Date.prototype.calcEaster = function(year:Number):Date {
if(year == null || year == undefined)year = this.getFullYear();
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;
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;
return new Date(year, month, day);
};

Example usage (default and with optional argument):


theDate = new Date();
trace(theDate.calcEaster());
// returns Sun Apr 12 00:00:00 GMT+0100 2009
trace(theDate.calcEaster(1999));
// returns Sun Apr 4 00:00:00 GMT+0100 1999