Problems with dates on Javascript


I have a form with two dates of an interval: and initial date and a final date. I have a function that makes some calculations based of these dates.
final date should be greather than initial date, so I would like to control this: if date2 is smaller or equal to date1, I would like to set date2 to the day after date1.

How can I do this in Javascript ?

Thank you.
LVL 15
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Date1: <input type="text" name="date1"  id="date1" size="6"  value="12/30/2011" /><br/>
Date2: <input type="text" name="date2"  id="date2" size="6"  value="12/15/2011" /><br/>
<a href="javascript:void(0);" onclick="javascript:CompareDates();">Compare Dates</a>
function CompareDates(){
  var date1 = new Date(document.getElementById("date1").value);
  var date2 = new Date(document.getElementById("date2").value);

  if (date2 < date1){
    date2.setDate(date1.getDate() + 1);



Open in new window

Right now, the function simply alerts the "final" date2 value.  You'll want to make this function either return the value, update the date2 input, etc.  This can easily be parameterized by passing in the IDs of the two fields you want to compare, etc.

Let me know if you have any questions.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gplanaAuthor Commented:
Excellent ! Thank you very much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.