3

Possible Duplicate:
How to add number of days to today’s date?

I need to get tomorrow and X number of days into the future using JavaScript and print in the format MM-DD-YYYY. For example, if I wanted to pass in the following parameters, I would like the start day to be tomorrow of the current date and the end date to be 31 days from now.

startDate: '02-02-2013',
endDate: '03-04-2013',

How can this be done?

Community
  • 1
  • 1
Cofey
  • 11,144
  • 16
  • 52
  • 74
  • How to get a date relative to today: http://stackoverflow.com/a/7937257/361684. How to print a date in a certain format: http://stackoverflow.com/a/14638191/361684. – gilly3 Feb 02 '13 at 00:43
  • http://stackoverflow.com/questions/3572561/javascript-set-date-10-days-in-the-future-in-this-format-dd-mm-yyyy-e-g-21-0 http://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date – Hontoni Feb 02 '13 at 00:45
  • There's no need to duplicate the answer given here: http://stackoverflow.com/a/3818198/361684. It gives you exactly what you are looking for. – gilly3 Feb 02 '13 at 01:37

2 Answers2

16

Date methods are smart enough to support this:

var date = new Date();
date.setDate(date.getDate() + 10 /*days*/);    

The date in this example will be 10 days ahead current date.

c-smile
  • 26,734
  • 7
  • 59
  • 86
4

This will help

Date.prototype.addDays=function(d) {
   return new Date(this.getTime() + d*86400000); // milliseconds
};
console.log(new Date().addDays(5);

or

Date.prototype.addDays=function(d) {
   this.setTime(this.getTime() + d*86400000);
   return this;
};
var time=new Date();
time.addDays(5);
console.log(time);
David Jones
  • 10,117
  • 28
  • 91
  • 139
zb'
  • 8,071
  • 4
  • 41
  • 68