Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a code like this to display next date in a html page.
XML
<form>
<input type="text" name="date">
</form>
<!-- note the position -->
<script type="text/javascript">
var currentTime = new Date();
var month = "00" + (currentTime.getMonth() + 1);
var day = "00" + (currentTime.getDate()+ 1);
var year = currentTime.getFullYear();
document.forms[0].date.value = month.slice(-2) + "/" + day.slice(-2) + "/" + year;
</script>
</body>
</html>


It is showing next date but it is problem with month change. If it is 07-31-2011 it has to go to 08-01-2011 but it is showing 07-32-2011.

I need it urgent.
Thanks for your help in advance.
Posted

1 solution

Hi, You have to use the setDate() function to get the correct date rollover, this will ensure the rest of the date (month, year) is also incremented if the day goes to the next month/year.

<form>>
<input type="text" name="date">
</form>
<!-- note the position -->
<script type="text/javascript">
var currentTime = new Date();
currentTime.setDate(currentTime.getDate()+1);
var month = "00" + (currentTime.getMonth()+1);
var day = "00" + (currentTime.getDate());
var year = currentTime.getFullYear();
document.forms[0].date.value = month.slice(-2) + "/" + day.slice(-2) + "/" + year;
</script>
</body>
</html>


By using the setDate() function you can quickly increment any number of days and get a valid date returned.

Also see setMonth(), setYear(), setFullYear().

Hope this helps
 
Share this answer
 
Comments
Member 8046452 21-Jul-11 12:02pm    
Thanks this is very helpful for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900