Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems comparing two dates

Status
Not open for further replies.

CHRISWYA

IS-IT--Management
Sep 14, 2004
19
GB
i am using the following javascript validation to ensure that a open date is greater than a close date. However i have just discovered that the .value check is not successfully working as it does not interpretate 01/10/04 as being greater than 28/09/04 for example. The check successfully occurs however for open/close dates from the same month.any suggestions please!?

if(theForm.p_close_date.value!=0)
{
if (theForm.p_open_date.value > theForm.p_close_date.value)
{
alert("The close date entered is invalid");
theForm.p_close_date.focus();
return false;
}
}
return true;
}
</script>
 
i have just discovered that the .value check is not successfully working as it does not interpretate 01/10/04 as being greater than 28/09/04

On the contrary - the value "check" and string comparison is working 100% - it is your logic and understanding that is not working ;o)

You are comparing a string with a string - which will not do any magic date checking. To do this, you have to convert the strings to a date object. You can either split the date up and pass the component DMY parts to a new date object yourself, or, (I believe) you can pass a formatted date string for it to parse.

Once you have your two date objects, comparing is a lot easier. You can call the getTime() method to return a number which you can use for comparison purposes (as the number bears a direct relationship to the date)... Something like this (once you have the date objects, of course):

Code:
if (open_date.getTime() > close_date.getTime()) {
   ...
}

Hope this helps,
Dan
 
Thanks for the advice i am very new to Javascript and dont quite follow all the advice given but thanks very much for you time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top