80

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

I'm confused, I found so many different approaches, and which of them is actually correct?

So what is the correct way to add day(s) to a given Date?

Community
  • 1
  • 1
iLemming
  • 34,477
  • 60
  • 195
  • 309

2 Answers2

210
date.setTime( date.getTime() + days * 86400000 );
nobody
  • 10,599
  • 4
  • 26
  • 43
  • 2
    This is what worked best for me working with _arbitrary_ dates. – bovender Aug 30 '13 at 17:00
  • 6
    +1 - `setDate` does not like adding days to a date, `setTime` is perfectly happy with adding number of days in milliseconds – dsaa Mar 27 '14 at 05:25
  • 6
    This must be the accepted answer as it is the only correct answer. – T3db0t Nov 20 '14 at 21:10
  • 2
    You'll have to also cast this to a new date: var newDate = new Date(date.setTime( date.getTime() + days * 86400000 )); – Tim Nov 13 '15 at 17:47
  • 45
    This logic does not take Daylight Savings into account. If we cross a "move forward an hour" or "move back an hour" boundary, the result will not be aligned on day boundaries. The solution is to round to the nearest day boundary by adding 12 hours and zeroing out the hours like so: `date.setTime(date.getTime() + 12 * 1000 * 60 * 60); date.setHours(0);` (...If you can believe it, I basically started using my account in earnest just this evening and answered 4 questions just so I could get enough reputation to write this comment. It matters to me that much, because I just ran into this problem!) – Danny McCue Nov 14 '15 at 07:32
  • Adding just 12 hours is not working when the time of the `date` object is before 12 AM. The date remains the same. The solution is first setting the hours to 22 for example, and then adding the 12 hours. – enenen Nov 29 '17 at 10:02
132

Note : Use it if calculating / adding days from current date.

Be aware: this answer has issues (see comments)

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

It should be like

var newDate = new Date(date.setTime( date.getTime() + days * 86400000 ));

user710502
  • 11,181
  • 29
  • 106
  • 161
  • yeah, but what if I add more than 31 days? – iLemming Aug 05 '11 at 22:42
  • 39
    It takes care of the logic for you. Flips to the next month, year, etc. It's pretty rad. – Tom Aug 05 '11 at 22:43
  • 31
    ^ +1 for using "rad" :D – finitenessofinfinity Sep 04 '14 at 09:52
  • 1
    +1 also a fan of "rad" :p – Nick Sep 25 '14 at 17:25
  • 13
    Fails if month or year are not the same... – Kelso Sharp Oct 23 '14 at 13:39
  • 14
    This does not work!! It only changes the day value of the current month... Sorry, it's not rad. The getTime method by "Nobody" works. – Del Dec 01 '14 at 17:42
  • 14
    Why do so many people accept this as the correct solution when, as suggested by others, it only works within the same month and year. – Alan Gee Jan 20 '15 at 12:12
  • 1
    @KelsoSharp can you share an example where this fails? I tried adding 100 days and it worked, thanks – Felipe Pereira Feb 09 '15 at 19:07
  • 1
    I tried adding 4 days to 2015-02-27 (as a date object) and it returned 2015-11-02 – Miles Feb 27 '15 at 13:59
  • 1
    "If the dayValue is outside of the range of date values for the month, setDate() will update the Date object accordingly. For example, if 0 is provided for dayValue, the date will be set to the last day of the previous month." ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate)) – Bennett McElwee Oct 21 '15 at 23:07
  • 2
    It _does_ work, you know. Try `var d=new Date(); d.setDate(32); console.log(d);` It displays a time at the beginning of next month, as you would expect from the documentation I referred to in my previous comment. – Bennett McElwee Oct 21 '15 at 23:12
  • Thanks, it helped me. – Ahesanali Suthar Aug 26 '16 at 10:38
  • Doesn't work at all when the month or year needs to be incresed as well! – motagirl2 Oct 30 '17 at 13:41
  • @motagirl2 That's because it days a number of _days_ - convert your months and years to days and then try again – James Cushing Nov 27 '17 at 16:44
  • I tried the code above using the last day of this month (3/31/2019). My program needs to be able to calculate 30, 60, and 90 days out--I got the expected results (4/30/19, 5/30/19, and 6/29/19, respectively). My only criticism is it also adjusts the input date variable, so I'll need to re-code if I want to preserve the original date. – Pizzor2000 Mar 29 '19 at 16:14
  • @Pizzor2000 what if you started with 11/30/2019. Would it still work? – Ε Г И І И О Oct 03 '19 at 06:00
  • @ΕГИІИО It still worked for me (Mon Dec 30 2009, Wed Jan 29 2020, Fri Feb 28 2020). – Pizzor2000 Oct 04 '19 at 22:15