I have the following javascript function that is supposed to add a week to a form date and store it in the form for next time the user clicks the link
function changeDate()> { var start = document.getElementById('start_date').value; var d = 7; var myDate=new Date(start); myDate.setDate(myDate.getDate()+parseInt(d)); document.getElementById('start_date').value = (myDate.getFullYear()) + '-' + (myDate.getMonth()+1) + '-' + (myDate.getDate()); } My form looks something like this
<?php>php $start_date = "2014-09-20"; ?> <form name="myForm" method="post"> <input type="text" name="start_date" id="start_date" value="<?php echo $start_date?>"/> <a href="#" onclick="changeDate();">Next< /a> </form> This script works fine incrementing a week each time the user clicks the hyperlink "Next", the problem happens with the month boundary. When the date is "2014-09-27" the script calculates the next date correctly and displays "2014-10-04". The problem happens on the next click, once the month have changed the next time the script fails. I don't understand why, I tried alerting the start_date and it is correct "2014-10-04", but once execute new Date command, it returns an invalid date.
The strange thing is that it doesn't fail on the month boundary (this actually gets calculated correctly) but it fails the next time. I thought it might be related to the current month but it doesn't matter, if I start with start_date set to "2014-11-03" it will continue adding correctly until I display the date "2014-12-01", but will fail the next time.
Any help is greatly appreciated