The algorithm for checking leap year is like the one below
1. if the year modulo 400 is 0 , its a leap year
2. else if the year modulo 100 is 0, then its not a leap year
3. else if the year modulo 4 is 0, its a leap year
4. else its not leap year
In plain JS, it will look something like the following one
function checkleapyear(year)
{
year = parseInt(year);
if(year%4 == 0)
{
if(year%100 != 0)
{
return true;
}
else
{
if(year%400 == 0)
return true;
else
return false;
}
}
return false;
}
I have seen this in numbers of web sites who are dealing with dates. But why bother when you can do it in the “Javascript way” - yes, take advantage of this fantastic language - heh heh. The following piece of code will do it for you
var dt = new Date(year+"/02/29");
if ("1"==dt.getMonth()) alert("leap year");
else alert("not leap year");
Hasin Hayder in JavaScript |
on Monday, February 25th, 2008 at 1:38 pm.
RSS 2.0 |
Leave a response | Trackback