Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Re: Date Validation


Message #1 by "Robert Davis" <robkdavis@h...> on Sat, 8 Feb 2003 12:30:00
> function testDate(nDay, nMonth, nYear)
> {
>  var dtEvent = new Date();
> 
>  dtEvent.setFullYear(nYear, nMonth, nDay) ;
> 
>   if(dtEvent.getYear() == nYear 
>      && dtEvent.getMonth() == nMonth 
>      && dtEvent.getDate() == nDay)
>   {
>      return true;
>   }
>   else
>   {
>      return false ;
>   }
> }
> 
> take care that testDate(29,01,1999) returns false
> 
> indeed you have to understant it like 1 month AND 29 days so
> this the date 29th February 1999 which doesn't exist...
> 
> you can change the format as you like by reversing the parameters..or 
else
> 

I wrote something similar, this seems to work great, but not fully tested:

function isDate(month,day,year) {
   var compareDate = new Date(month + "/" + day + "/" + year);
   var compareMonth = compareDate.getMonth() + 1;
   return compareMonth == month;
}

  Return to Index