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

Start and End Date comparison error

Status
Not open for further replies.

akaballa123

Technical User
Apr 29, 2008
46
US
Hi,

I am trying to create a date check scenario to see if my start date greater than my end date:

This is the code im using

Code:
var cStartD = myForm.startDate;
   var cEndD = myForm.endDate;
   
   var startD_array = cStartD.value.split("/");
   var endD_array = cEndD.value.split("/");


 if(parseInt(startD_array[0]) > parseInt(endD_array[0]))
   {
        alert('Start Date is ahead of the End Date.  Recheck the dates please!');
        cStartD.focus();
        return false;
   }
   else if(parseInt(endD_array[0]) == parseInt(startD_array[0])  )
   {
       if(parseInt(startD_array[1]) > parseInt(endD_array[1])   )
       {
          return false;
          alert('Start Date is ahead of the End Date.  Recheck the dates please!');
          cStartD.focus();
       }
       else if(parseInt(endD_array[1]) == parseInt(startD_array[1]) )
       {
          if(parseInt(startD_array[2]) > parseInt(endD_array[2])   )
          {
             return false;
             alert('Start Date is ahead of the End Date.  Recheck the dates please!');
             cStartD.focus();
          }
       }
   }

This scenario is placed within a checkform function that is called the users clicks on the submit button of the form.

My first case works where a error is outputted if the start year is greater than the end year. But, the other two are not responding. Like when I click submit I do not get an error and neither does the form get submitted.

 
The logic behind them is dodgy anyway. For example, your logic would flag 20/02/2008 as being ahead of 10/03/2008 - which clearly it is not.

Your best bet is to parse the date into number of msecs and simply compare the msecs values.

This also has the benefit of fixing dodgy dates such as 35/02/2008.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top