Add to current date with javascript and move multiple months
I need to use basic javascript here and am not very good at it. I don't want to use jquery (although I would rather)
I need to get the current date via javascript, add 2 days to it, and then display the new date (given 2 extra days). This means that within 30 or 31 months, the month should roll over, just like the year December 30th.
Thanks to this question and Samuel Meadows, I can get the current date. I can add 2 days with no problem. But I can't work out how to properly roll the months (and the year).
Samuel's code:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = mm+'/'+dd+'/'+yyyy;
document.write(today);
Any help would be greatly appreciated.
Thank!
+3
source to share
3 answers
Convert the number of days to milliseconds and then add them to the appropriate date http://jsfiddle.net/Mn5Wz/
+2
source to share