PDA

View Full Version : Compare date not working??


buckbeak
10-11-2002, 07:38 AM
What is wrong with my codes? It's not working!

function check_date(){
date1 = document.forms[0].DateField1.value;
date2 = document.forms[0].DateField2.value
if (new Date(date1) > new Date(date2)){alert("To Date cannot be less than from date!");return false;}
else {return true;}

I want to check that FROM date cannot be greater than TO date??

The value of date1(format) = mm/dd/yy

Please help!

Peanut
10-11-2002, 09:58 AM
The only date checking I've done would only work if I moved the year, month, and day of the date(s) needing validation into a seperate variable. The format had to be year,month,day.
eg...

/ form value effdate is in format dd/mm/yyyy

var Effdatechk = document.form.effdate.value;
var Effday = Effdatechk.substring(0,2);
var Effmonth = Effdatechk.substring(3,5);
var Effyear = Effdatechk.substring(6,10);

var Effcheck = new Date(Effyear,Effmonth,Effday);

/ then the second date

var Startdatechk = df.startdate.value;
var Startday = Startdatechk.substring(0,2);
var Startmonth = Startdatechk.substring(3,5);
var Startyear = Startdatechk.substring(6,10);

var Startcheck = new Date(Startyear,Startmonth,Startday)

/ then check 'em...

if (Endcheck < Startcheck){
alert ("End date is less than start date.");
return false;
}


This may well be overkill, I'm not too experienced with JS, but it works !

... Peanut :D

glenngv
10-11-2002, 10:09 AM
Peanut, Date object has methods getDate, getMonth, and getFullYear to do what you coded.

buckbeak, did you have a typo when you post the code?

function check_date(){
date1 = document.forms[0].DateField1.value;
date2 = document.forms[0].DateField2.value
if (new Date(date1) > new Date(date2)){alert("To Date cannot be less than from date!");return false;}
else {return true;}
}