Problems with dates on Javascript

Hi.

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
gplanaAsked:
Who is Participating?
 
nap0leonConnect With a Mentor Commented:


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>
<script>
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);
  }

  alert('final2='+date2);
}

</script>

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.
0
 
gplanaAuthor Commented:
Excellent ! Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.