Advertisement

08.24.2005 at 07:41PM PDT, ID: 21539279
[x]
Attachment Details

Comparing 2 Dates in Javascript -  Date comparison in Javascript

Asked by frankmorrison in JavaScript

Tags: javascript, date, compare, comparison

I'm trying to compare 2 dates on a form. What is the simplest way to do this?  Here's what I have so far:

<html>


<script>

function Compare() {


if (document.frmTest.startDate.value > document.frmTest.endDate.value){
alert ("Start Date is greater");
}

if (document.frmTest.startDate.value < document.frmTest.endDate.value){
alert ("End Date is greater");
}

if (document.frmTest.startDate.value == document.frmTest.endDate.value){
alert ("Dates are the same");
}


}
</script>


<body>


<form name="frmTest">
      Start Date: <input type="text" name="startDate" size="12">
      <br>
      End Date: <input type="text" name="endDate" size="12">
      <a href="javascript:Compare();">Compare</a>

</form>

</body>

</html>

If the user enters "8/1/2005" for the Start Date and "8/2/2005" for the End Date, the script works. But if the user enters "8/1/2005" and "11/01/2005" the script does not work.Start Free Trial
[+][-]08.24.2005 at 09:04PM PDT, ID: 14749067

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: javascript, date, compare, comparison
Sign Up Now!
Solution Provided By: Thogek
Participating Experts: 4
Solution Grade: A
 
 
[+][-]08.24.2005 at 09:11PM PDT, ID: 14749099

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.25.2005 at 12:47AM PDT, ID: 14749811

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32