evanburen
evanburen

Reputation: 309

Adding Days to Current Date in javascript

All I want to do is to add 20 days to the current date. I need the results in mm/dd/yyyy format. Today is 05/15/2014 but this displays 05/35/2014 which of course is not a valid date.

var myDate = new Date();
alert((myDate.getMonth() + 1) + "/" + (myDate.getDate() + 20) + "/" +   myDate.getFullYear());

Upvotes: 0

Views: 87

Answers (1)

MrCode
MrCode

Reputation: 64526

Use the setDate() method of your Date object, passing the current date plus the number of days to add.

var myDate = new Date();
myDate.setDate(myDate.getDate() + 20);

alert((myDate.getMonth() + 1) + "/" + (myDate.getDate()) + "/" +   myDate.getFullYear());

Outputs

6/4/2014

Upvotes: 1

Related Questions