Thursday, September 27, 2007

Check date in Java Script

here is my date check javascript.

function leapyearcheck(year)
{

if(year.value % 4 != 0)
{
return 0;
}

else
{
if(year.value % 100 != 0)
{
return 1; //leap year
}

else
{

if(year.value % 400 != 0)
{
return 0
}

else
return 1; //leap year

}

}
}


function datecheck(day,month,year) //call this function
{
//alert(day.value);

if(!leapyearcheck(year))
{
//alert("Not Leap Year");

if(month.value == 2)
{
if(day.value > 28)
{
alert("Check the Day and Month");
return false;
}

}
}

else

{

//alert("Leap Year");
if(month.value == 2)
{
if(day.value > 29)
{
alert("Check the Day and Month");
return false;
}
}

}


Thanks to HasinHyder for a tricky one..

var dt = new Date(year+"/02/29");
if (dt.getMonth()=="1")
alert (year + " is a leap year");
else
alert(year + " is not a leap year")



3 comments:

Unknown said...

salauddin,

You could check it like the following one :D no need for a page full of JS

var dt = new Date(year+"/"+month+"/"+day);
if (dt.getMonth()=="1")
alert (year + " is a leap year");
else
alert(year + " is not a leap year")

salahuddin66 said...

:)

I sould use the JavaScript Date Object.

Unknown said...

a small correction S66, here it goes

var dt = new Date(year+"/"+month+"/"+day);

should be

var dt = new Date(year+"/02/29");

:D