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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two dates

Status
Not open for further replies.

plarsen

Programmer
Jul 30, 2002
27
DK
Hi

I have a bit of code that compares two dates, one date from a form and one date from New Date().

Here is the code i wrote:

var d = new Date();
var da = (d.getDate() + "-" + (d.getMonth()+1) + "-" + d.getYear());
if (document.form1.real.value < da) {
alert(&quot;The selected date is in the past!&quot;);
}

The problem is as followed:

if document.form1.real.value is 19-9-2003 and
da is 2-9-2003 then the alert-line is executed. What can't be right, because 19th of September 2003 i not smaller than 2nd of September 2003! Is it?

the same proplem occurs if document.form1.real.value is between 10th and 19th.

I hope someone can help me!

Kind regards

Peter Larsen
 
Instead of converting the current day's date to a string, try converting the other date to a date format by using the Date.parse method as follows:

var todaysDate = new Date();
var otherDate = new Date();
otherDate.setTime(Date.parse(document.form1.real.value);

if (otherDate < todaysDate) {
alert (&quot;The selected date is in the past!&quot;);
}


That should work.

-kaht
 
It worked just fine.

Thanks for the help!

Kind regards

Peter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top