PDA

View Full Version : Ext.util.EasterDate

wm003
25 Mar 2008, 10:06 PM
For calculation of my national holidays i needed to calculate the easter Date of each year.

The following Function returns a Date-Object of Easter-Sunday of a given year. If no year is given the routine will use the current year.

[UPDATE 2008/03/28]
Added option to add/subtract a number of days from the calculated eastersunday

e.g.
Ext.util.EasterDate(2008) returns a DateObject of 2008/03/23
Ext.util.EasterDate(2008,5) returns a DateObject of 2008/03/28
Ext.util.EasterDate(2008,-3) returns a DateObject of 2008/03/20

Ext.util.EasterDate = function(year, plusDays) {
if (typeof year === "undefined") {
year = new Date().getFullYear();
}
year = parseInt(year,10);

if (typeof plusDays === "undefined") {
plusDays = 0;
}
plusDays = parseInt(plusDays,10);

//difference to first sunday after first fullmoon after beginning of spring
var a = year % 19;
var d = (19 * a + 24) % 30;
var diffDay = d + (2 * (year % 4) + 4 * (year % 7) + 6 * d + 5) % 7;
if ((diffDay == 35) || ((diffDay == 34) && (d == 28) && (a > 10))) {
diffDay -= 7;
}

var EasterDate = new Date(year, 2, 22); //beginning of spring
EasterDate.setTime(EasterDate.getTime() + 86400000 * diffDay + 86400000 * plusDays);
return EasterDate;
};

mystix
25 Mar 2008, 10:19 PM
interesting plugin, but

"Ext can only calculate DateObjects between 1970 and 2099"

i didn't know that... :-?

are you sure? :)

wm003
25 Mar 2008, 10:53 PM
Mmh, i read about this on the net somewhere, that javascript should have these restrictions on 32-Bit machines....anyway i did some more testing and it also seems to work with other years such as 3100 and 1960....

I deleted the exception-catch out of the code, so it should work for any year now.

Thanks for pointing that out :)

mystix
25 Mar 2008, 11:29 PM
Mmh, i read about this on the net somewhere, that javascript should have these restrictions on 32-Bit machines....anyway i did some more testing and it also seems to work with other years such as 3100 and 1960....

I deleted the exception-catch out of the code, so it should work for any year now.

Thanks for pointing that out :)

do you happen to have the URL to the article?
might be good to know. ;)

wm003
25 Mar 2008, 11:54 PM
do you happen to have the URL to the article?
might be good to know. ;)

e.g:
http://jneumeyer.com/Chap10Notes.htm

He talks only about the 1970 thing

The original javascript routine i had adopted the code above also had this restriction set. And i never doubt about that myself (maybe because i never happened to use any date outside this range)
The quote about the 32-Bit machines i read in the PHP-Docs for easter_date.

wm003
27 Mar 2008, 11:28 PM
Updated the code with optional number of days to add/subtract from the calculated eastersunday