How to add days to current Date
using JavaScript? Does JavaScript have a built in function like .NET's AddDay()
?

- 13,617
- 16
- 88
- 129

- 16,995
- 3
- 18
- 8
55 Answers
You can create one with:-
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = new Date();
console.log(date.addDays(5));
This takes care of automatically incrementing the month if necessary. For example:
8/31 + 1 day will become 9/1.
The problem with using setDate
directly is that it's a mutator and that sort of thing is best avoided. ECMA saw fit to treat Date
as a mutable class rather than an immutable structure.

- 187,200
- 47
- 362
- 445

- 187,081
- 35
- 232
- 306
-
6These solutions do not handle the case when you need to know whether the hour will be different after `days` many 24-hour days. Adjust `getDate` / `setDate` / `days` to `getTime` / `setTime` / `days * 24*60*60*1000` if you want time to detect DST. – Andy Novocin Jun 23 '14 at 14:53
-
30Simplified: `Date.prototype.addDays=function(d){return new Date(this.valueOf()+864E5*d);};` – Duncan Oct 15 '14 at 17:51
-
50@Duncan Are you sure this is the same as the above? This one simply adds 24 hours, but [one day is not always 24 hours](https://en.wikipedia.org/wiki/Daylight_saving_time). I guess the question is how to increment the date part by 1 day, without changing the time part. – Tamás Bolvári Dec 19 '15 at 06:41
-
@Tamás Bolvári: see the answer I supplied below. :) – Duncan Dec 23 '15 at 17:38
-
155```864E5``` for some reason I prefer to write ```24*60*60``` in my code :) – Mars Robertson Jan 21 '16 at 20:53
-
1@MichalStefanow That always comes off to me as: "I couldn't be bothered to do the math myself". To each their own though. :) – Duncan Feb 11 '16 at 19:54
-
90Guys, do not use method of adding 864E5 because this does not take daylight saving difference where days can be 23 or 25 hours. – sbrbot Jul 25 '16 at 21:44
-
26For those of you worried about Daylight Savings -- Don't. These algorithms change the underlying date, not how the date is interpreted. DST is implemented in the getters and setters of the date -- the getters to subtract an hour in the summer, and the setter to add that hour back during the summer to normalize the time. But only when using an absolute hour -- relative hours don't need to be interpreted. This is why date math works. IMHO... – Gerard ONeill Nov 22 '16 at 16:53
-
2While it might be a matter of principle to not mutate the date on which the method is called, it's not sensible. All *set* methods mutate the date and return the time value, for this method to not change the date and to return a new date object instead of the time value is not pragmatic. – RobG Jan 28 '17 at 14:13
-
3Just to provide an example for @sbrbot's point: Once you convert to microseconds (with valueOf() or getTime()), DST will start to mess up your calculations. e.g., using @Duncan's prototype try this and you'll lose an hour: `d = new Date( 2016, 10, 4 )` `d.addDays(3)` – Colin Apr 25 '17 at 14:33
-
is it working with float number like: `addDays(3.0)` ?@AnthonyWJones – always-a-learner Jul 17 '17 at 10:25
-
This is not working if days are a decimal number: Works on Chrome but Firefox does not change hours/minutes/seconds. – Mick May 16 '18 at 15:10
-
Hi very nice s oltion although the date doesn't actually change, so if u call date.addDays(5); date.addDays(5); the result will be the same, I tried to make the solution Date.prototype.addDays=function(days){var date = new Date(this.valueOf());date.setDate(date.getDate()+days);this.setDate(date.getDate());return this;} but it doesn't update the month, do you know how to fix this? – B''H Bi'ezras -- Boruch Hashem Nov 08 '18 at 10:40
-
3DAYLIGHT SAVINGS - sorry for caps, but it's important and needs to stand out :) – Chris Kemp Dec 21 '18 at 13:55
-
2That should be `24*60*60*1000` – jameshfisher Mar 23 '19 at 21:48
-
Some minutes are not 60 secondes ... and some year are not 365,25 days ... just saying – sebastienbarbier Nov 20 '19 at 05:59
-
1Sometimes the solution here does not work – Traoré Moussa Mar 20 '21 at 05:36
-
I got some weird results with this function and figured out that it's when passing in `days` as a string. So I would add a `parseInt()` to the solution to make sure that doesn't happen. It's hard to troubleshoot, there won't be an error but the returned date for me was sometime 9 months ahead when `days==3`, but it all worked when `days==2`. No idea why, but `parseInt()` will care for that. – Ulli Mar 30 '21 at 04:55
-
@Duncan's comment is correct if (and only if) your date represents a UTC date/time. AnthonyWJones's answer is correct if (and only if) your date represents local time. The thing to understand is that a JavaScript Date represents an absolute moment in time, and they DO NOT have a time zone, so you must choose the correct method to use based on the time zone that you know (in your mind) the date represents. – Qwertie Apr 15 '21 at 00:01
-
1Extending native prototypes is an anti pattern: https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice – Ole Aug 12 '21 at 02:20
-
This solution is not correct if the local time zone crosses a daylight saving time switch, e.g. in a CEST firefox: `d = new Date("Mon, 25 Oct 2021 00:00:00 GMT")` `d.addDays(7).toUTCString()` yields `"Mon, 01 Nov 2021 01:00:00 GMT"` – Benni Oct 24 '21 at 16:47
-
1Typescript doesn't like this one I'm afraid – JimmyTheCode Dec 08 '21 at 16:19
-
Why not to do it short? `Date.prototype.addDays = function(days) { this.setDate(this.getDate() + days); }` – Enriqe Sep 06 '22 at 08:16
-
Why are mutators are best avoided? – user7868 Jul 07 '23 at 00:32
-
@Enriqe Because that would be mutation of the object which is undesirable. – AnthonyWJones Aug 16 '23 at 10:07
-
1@user7868 dates are essentially a scalar value, it represents a fixed point in time. Consider calling a function that accepts a date. The function should treat the date as immutable, it can't know how else that date is used in code and so should not change it. – AnthonyWJones Aug 16 '23 at 10:21
Correct Answer:
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
Incorrect Answer:
This answer sometimes provides the correct result but very often returns the wrong year and month. The only time this answer works is when the date that you are adding days to happens to have the current year and month.
// Don't do it this way!
function addDaysWRONG(date, days) {
var result = new Date(); // not instatiated with date!!! DANGER
result.setDate(date.getDate() + days);
return result;
}
Proof / Example
// Correct
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
// Bad Year/Month
function addDaysWRONG(date, days) {
var result = new Date();
result.setDate(date.getDate() + days);
return result;
}
// Bad during DST
function addDaysDstFail(date, days) {
var dayms = (days * 24 * 60 * 60 * 1000);
return new Date(date.getTime() + dayms);
}
// TEST
function formatDate(date) {
return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
}
$('tbody tr td:first-child').each(function () {
var $in = $(this);
var $out = $('<td/>').insertAfter($in).addClass("answer");
var $outFail = $('<td/>').insertAfter($out);
var $outDstFail = $('<td/>').insertAfter($outFail);
var date = new Date($in.text());
var correctDate = formatDate(addDays(date, 1));
var failDate = formatDate(addDaysWRONG(date, 1));
var failDstDate = formatDate(addDaysDstFail(date, 1));
$out.text(correctDate);
$outFail.text(failDate);
$outDstFail.text(failDstDate);
$outFail.addClass(correctDate == failDate ? "right" : "wrong");
$outDstFail.addClass(correctDate == failDstDate ? "right" : "wrong");
});
body {
font-size: 14px;
}
table {
border-collapse:collapse;
}
table, td, th {
border:1px solid black;
}
td {
padding: 2px;
}
.wrong {
color: red;
}
.right {
color: green;
}
.answer {
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th colspan="4">DST Dates</th>
</tr>
<tr>
<th>Input</th>
<th>+1 Day</th>
<th>+1 Day Fail</th>
<th>+1 Day DST Fail</th>
</tr>
<tr><td>03/10/2013</td></tr>
<tr><td>11/03/2013</td></tr>
<tr><td>03/09/2014</td></tr>
<tr><td>11/02/2014</td></tr>
<tr><td>03/08/2015</td></tr>
<tr><td>11/01/2015</td></tr>
<tr>
<th colspan="4">2013</th>
</tr>
<tr>
<th>Input</th>
<th>+1 Day</th>
<th>+1 Day Fail</th>
<th>+1 Day DST Fail</th>
</tr>
<tr><td>01/01/2013</td></tr>
<tr><td>02/01/2013</td></tr>
<tr><td>03/01/2013</td></tr>
<tr><td>04/01/2013</td></tr>
<tr><td>05/01/2013</td></tr>
<tr><td>06/01/2013</td></tr>
<tr><td>07/01/2013</td></tr>
<tr><td>08/01/2013</td></tr>
<tr><td>09/01/2013</td></tr>
<tr><td>10/01/2013</td></tr>
<tr><td>11/01/2013</td></tr>
<tr><td>12/01/2013</td></tr>
<tr>
<th colspan="4">2014</th>
</tr>
<tr>
<th>Input</th>
<th>+1 Day</th>
<th>+1 Day Fail</th>
<th>+1 Day DST Fail</th>
</tr>
<tr><td>01/01/2014</td></tr>
<tr><td>02/01/2014</td></tr>
<tr><td>03/01/2014</td></tr>
<tr><td>04/01/2014</td></tr>
<tr><td>05/01/2014</td></tr>
<tr><td>06/01/2014</td></tr>
<tr><td>07/01/2014</td></tr>
<tr><td>08/01/2014</td></tr>
<tr><td>09/01/2014</td></tr>
<tr><td>10/01/2014</td></tr>
<tr><td>11/01/2014</td></tr>
<tr><td>12/01/2014</td></tr>
<tr>
<th colspan="4">2015</th>
</tr>
<tr>
<th>Input</th>
<th>+1 Day</th>
<th>+1 Day Fail</th>
<th>+1 Day DST Fail</th>
</tr>
<tr><td>01/01/2015</td></tr>
<tr><td>02/01/2015</td></tr>
<tr><td>03/01/2015</td></tr>
<tr><td>04/01/2015</td></tr>
<tr><td>05/01/2015</td></tr>
<tr><td>06/01/2015</td></tr>
<tr><td>07/01/2015</td></tr>
<tr><td>08/01/2015</td></tr>
<tr><td>09/01/2015</td></tr>
<tr><td>10/01/2015</td></tr>
<tr><td>11/01/2015</td></tr>
<tr><td>12/01/2015</td></tr>
</tbody>
</table>

- 25,926
- 9
- 53
- 65

- 12,546
- 3
- 21
- 32
-
@bzlm, Yes essentially the same. It just doesn't modify the Date prototype. I also wanted to point out the flaws in the main answer. – sparebytes Oct 30 '13 at 19:27
-
The accepted answer was edited a fortnight ago to point out its flaws. Do you think it needs further editing? If so, how? – bzlm Oct 30 '13 at 19:33
-
14@bzlm: Yes, I believe the note should read "this approach fails if the 'from' date is not in the same year **or month** as the **current date**". I still worry that users will glance over the answer and not read the warning since it doesn't stand out. Thanks. – sparebytes Oct 30 '13 at 19:40
-
At line result.setDate(date.getDate() + days); you need to change date.getDate() to result.getDate() if you are passing value not the date exactly .. otherwise there is an exception – Usman Younas Apr 15 '15 at 13:23
-
@UsmanY, I updated the code as you suggested. You only got that exception because you didn't pass a Date object into the function. Now the function will accept date-formatted strings. `"mm/dd/yyyy"` – sparebytes Apr 16 '15 at 14:44
-
@sparebytes i have up-voted though :) and thanks for taking care of this, I just try to help others if they get same exception. so things should be open to people – Usman Younas Apr 16 '15 at 15:15
-
Great answer on a confusing SO thread. It would be really great to add to the answer *why* the wrong way fails behind the scenes. – Joel Apr 19 '15 at 16:54
-
@bzlm please re-edit the correct answer. Now it's ok. This is leading to confusing notes about the correct answer saying that won't be useful. – Sebastian Apr 30 '15 at 16:10
-
4I think, even if this work it is not correct. There is no such constructor for Date: _var result = new Date(date);_, see [http://www.w3schools.com/js/js_dates.asp](http://www.w3schools.com/js/js_dates.asp). Even if this usually work, sometimes can lead to wrong results because of conversion to string and vice versa. It should be _var result = new Date(date.getTime());_ – Marcin Nov 10 '15 at 10:22
-
1@Marcin, I agree that `new Date(date.getTime());` is better but I don't think there is any case where `new Date(date)` will give a wrong answer due to string conversion. If anyone can provide an example I'd like to see it. How crazy would it be for a date constructor to not read the string the same way it is formatted to by default. From what I understand, the user's local timezone and date format settings should have no affect on the correctness of this function. – sparebytes Apr 20 '16 at 22:06
-
Dude this answer is awesome! I have been doing the incorrect approach by incrementing the `1st of June` with `1 day` and getting the `2nd of May` which was driving me crazy... – Martin Shishkov May 30 '16 at 21:31
-
This answer works. I wrote it first as new Date() and the value was off by a month if the starting date wasn't the current month – boilers222 Feb 06 '17 at 17:32
-
-
for current Date, why not just `daysToAdd = 10;` `date = new Date(Date.now() + (1000 * 60 * 60 * 24 * daysToAdd));` – Ankit Balyan Jul 30 '17 at 22:35
-
2@AnkitBalyan, See other answers. It may not work correctly due to Daylights Savings Time – sparebytes Jul 31 '17 at 16:49
-
This is not working if days are a decimal number: Works on Chrome but Firefox does not change hours/minutes/seconds. – Mick May 16 '18 at 15:10
-
Has anything changed in these years? JSFiddle shows all green for `+1 Day DST Fail ` and all red for `+1 Day Fail`. Chrome/Edge v94. On the other note - Such a simple operation requires me to read so much information and a whole community to solve. :facepalm: – Janis Veinbergs Oct 04 '21 at 10:16
-
Sorry, forgot to attach screenshot for previous comment: https://i.imgur.com/rtDPUdv.png Actually if I look at the results - the `+1 Day Fail` solution is incorrect test for someone with different locale settings that lives outside of US... – Janis Veinbergs Oct 04 '21 at 10:23
-
Can someone explain the difference in code between the correct and incorrect version? – Jimmy Dillies Oct 31 '21 at 21:52
-
1@JimmyDillies, the difference is `new Date();` vs `new Date(date)`. The first creates with the **current** year, month, and day; and then changes the day. The second creates a date with the **given** year, month, day, and then changes the day. – sparebytes Nov 01 '21 at 22:21
-
var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);
Be careful, because this can be tricky. When setting tomorrow
, it only works because its current value matches the year and month for today
. However, setting to a date number like "32" normally will still work just fine to move it to the next month.

- 399,467
- 113
- 570
- 794
-
I did edit some, but you can see that this version still predated his answer. Ah, well. – Joel Coehoorn Feb 19 '09 at 01:19
-
1Yeah? But what is this? (ran on March 31st, 2010): today = new Date(); tomorrow = new Date(); tomorrow.setDate(today.getDate()+1); alert(tomorrow.getMonth()); Says "3". alert(tomorrow); is correct... Why??? – d-_-b Mar 31 '10 at 02:44
-
19
-
9Why the need to create 2 separate date objects? Why not simply use the same date object: `var d = new Date(); d.setDate( d.getDate() + 1 );`? – Joseph Silber Mar 25 '12 at 15:03
-
13This approach doesn't work across years. If your starting date is from a few years ago, `getDate()` returns the _day of that year_. Then, calling `setDate` sets the day in the _current year_. So it is *NOT* a good general solution. [@AnthonyWJones's answer](http://stackoverflow.com/a/563442/24874) actually works correctly. – Drew Noakes Oct 14 '13 at 10:55
-
2Creating two `Date` objects with `new` will fail if performed just when switching between months. `today.getDate()` would return the last day of the month, while `tomorrow`s month would already be in the next month. So `tomorrow` would end up completely wrong. – Dag Høidahl Oct 20 '16 at 12:02
-
8@DrewNoakes—your assertion that it doesn't work across years is wrong. *getDate* returns the day in the month, not "day of that year". E.g. `var d = new Date(2015,11,30);d.setDate(d.getDate() + 370)` gives 3 Jan 2017 which crosses 2 years. – RobG Dec 06 '16 at 23:09
-
*"Be careful, because this can be tricky. When setting "tomorrow", it only works because it's current value matches the year and month for "today"."* What do you mean by this? Adding dates to a day works just fine across months (or even years)... I suspect I'm just not reading what you wrote the way you meant it. – T.J. Crowder Nov 21 '20 at 16:02
-
I don't know why this answer is not more high in the answers. Much more clean and simple. – Caio V. Jul 29 '21 at 19:35
These answers seem confusing to me, I prefer:
var ms = new Date().getTime() + 86400000;
var tomorrow = new Date(ms);
getTime() gives us milliseconds since 1970, and 86400000 is the number of milliseconds in a day. Hence, ms contains milliseconds for the desired date.
Using the millisecond constructor gives the desired date object.

- 2,113
- 2
- 14
- 7
-
61This solution doesn't take daylight savings into account. So, for example, this will return the same date, 23 hours later: `new Date(new Date('11/4/2012').getTime() + 86400000)` – Noah Harrison Mar 20 '12 at 14:55
-
7@NoahMiller The problem which you bring up could be a feature not a bug! Adding 24 hours per day is sometimes the right thing to do, with the goal of knowing the resulting time based on DST. The date that your example returns has a time value of 11pm on November 4th which is what 24 hours later is on that particular day. The original poster asked about datetime which would seem to indicate some desire for correct times of the day. This answer is correct if you are in the case when your goal is the time 24 hours later. – Andy Novocin Jun 23 '14 at 15:12
-
6I agree Noah, var d2 = new Date(d1.valueOf() + 24 * 60 * 60 * 1000) does what it says, adds a full day worth of ticks to a date. – Corey Alix Jul 18 '14 at 16:44
-
9This is absolutely correct for some cases (for example for 1 day cookies) and a simpler solution than most of the others. I dont get why it has so many downvotes and so few upvotes :/ – Matteo B. Nov 06 '14 at 12:05
-
1Agree with Matmarbon that Corey Alix's answer is better. I implemented the highest ranked answer from Sparebytes but got erroneous results; Corey Alix's answer worked – Rory Nov 13 '20 at 00:28
-
2This answer is correct if (and only if) your date represents a UTC date/time or if you only want to add 24-hour days. Some other answers (AnthonyWJones's) are correct if (and only if) your date represents local time. The thing to understand is that a JavaScript Date represents an absolute moment in time, and they DO NOT have a time zone, so you must choose the correct method to use based on the time zone that you know (in your mind) the date represents. – Qwertie Apr 15 '21 at 00:22
-
That's a highly underappreciated solution! It solves the problem for the different months! – Lopofsky Apr 27 '22 at 10:21
My simple solution is:
nextday=new Date(oldDate.getFullYear(),oldDate.getMonth(),oldDate.getDate()+1);
this solution does not have problem with daylight saving time. Also, one can add/sub any offset for years, months, days etc.
day=new Date(oldDate.getFullYear()-2,oldDate.getMonth()+22,oldDate.getDate()+61);
is correct code.

- 6,169
- 6
- 43
- 74
-
13
-
Doesn't work on the last day of any month, as you say. Makes this unusable on 12 days of the year. Sounds like a nightmare to debug!!! – Drew Noakes Oct 14 '13 at 10:59
-
7No Drew, it is usable for all days on year. You can put date offset bigger than 31 or month offset bigger than 12 and this function will recalculate it as day in next month or month in next year. So for example: nextday=new Date(oldDate.getFullYear(),oldDate.getMonth(),oldDate.getDate()+40); is perfectly well code. – sbrbot Nov 11 '13 at 07:30
-
-
1I Agree, use this way. We just had a bug because of daylight saving time because we were using `setDate`. – Jelle den Burger Apr 11 '17 at 06:41
-
I have tried the method above and got strange results when adding 1000s of days. It gets solved by adding all the parameters of the Date constructor: Year, Month, Day,Hour, Minute,Second – Alex Perrin Jan 04 '18 at 23:19
-
Here is the way that use to add days, months, and years for a particular date in Javascript.
// To add Days
var d = new Date();
d.setDate(d.getDate() + 5);
// To add Months
var m = new Date();
m.setMonth(m.getMonth() + 5);
// To add Years
var y = new Date();
y.setFullYear(y.getFullYear() + 5);

- 1,901
- 12
- 17
Try
var someDate = new Date();
var duration = 2; //In Days
someDate.setTime(someDate.getTime() + (duration * 24 * 60 * 60 * 1000));
Using setDate() to add a date wont solve your problem, try adding some days to a Feb month, if you try to add new days to it, it wont result in what you expected.

- 1,117
- 9
- 19
-
32No, this should not be marked as the correct answer since this solution assumes that every day has 24*60*60*1000 seconds but it does not (daylight saving)! – sbrbot Jan 12 '13 at 09:06
-
Any evidence about the 'Feb' problem with `setDate()`? Is it this: http://stackoverflow.com/questions/5497637/javascript-setdate-returning-wrong-dates – Brent Bradburn Feb 04 '13 at 03:21
-
12+1 This SHOULD be marked as the correct answer. I believe that "daylight saving" is about **presentation** and not about **value**, which is just the number of milliseconds. From **value**-point of view - day is CONST number of millisecs, while in terms of **presentation** it may vary. – disfated Mar 17 '15 at 08:17
-
1@disfated—this is not the correct answer. The day going out of daylight saving has 25 hours, but this method only adds 24 so the date will be the same. Using 24hrs to represent a day works if UTC methods are used instead, but why bother when using *setDate* is more convenient? ;-) – RobG Dec 06 '16 at 23:13
Just spent ages trying to work out what the deal was with the year not adding when following the lead examples below.
If you want to just simply add n days to the date you have you are best to just go:
myDate.setDate(myDate.getDate() + n);
or the longwinded version
var theDate = new Date(2013, 11, 15);
var myNewDate = new Date(theDate);
myNewDate.setDate(myNewDate.getDate() + 30);
console.log(myNewDate);
This today/tomorrow stuff is confusing. By setting the current date into your new date variable you will mess up the year value. if you work from the original date you won't.

- 4,605
- 3
- 28
- 39

- 4,142
- 6
- 39
- 52
-
10Reading all the answers around until I find this jewel here. Now that makes sense. Amazing that the today/tomorrow thing was copied in almost all the answers, when it does not make sense at all and it is not "for readibility and clarity" as the author says in the most upvoted answer -in the most upvoted comment-, it is confusing and a bad practice and wrong – Cesc Aug 08 '14 at 06:01
The simplest approach that I have implemented is to use Date() itself. `
const days = 15;
// Date.now() gives the epoch date value (in milliseconds) of current date
nextDate = new Date( Date.now() + days * 24 * 60 * 60 * 1000)
`
-
1You should use NicoESIEA's answer if you care about DST (which you should) – Mark Fisher Mar 26 '21 at 16:23
-
3This by far is the most elegant solution to the problem because it takes care of the fact that it handles the nasty situation of setting days beyond that of the month. You could literally right the function like...function addTime(years, month, day). If you want to go crazy with it you could add the hours, minutes, seconds and the milliseconds. I used to do it a lot in Unity for keeping track of the time in game because it used the Epoch method. – Dave Jul 13 '21 at 17:16
int days = 1;
var newDate = new Date(Date.now() + days * 24*60*60*1000);
var days = 2;
var newDate = new Date(Date.now() + days * 24*60*60*1000);
document.write('Today: <em>');
document.write(new Date());
document.write('</em><br/> New: <strong>');
document.write(newDate);

- 13,940
- 35
- 121
- 205
If you can, use moment.js. JavaScript doesn't have very good native date/time methods. The following is an example Moment's syntax:
var nextWeek = moment().add(7, 'days');
alert(nextWeek);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js"></script>
Reference: http://momentjs.com/docs/#/manipulating/add/

- 4,433
- 2
- 26
- 37

- 844
- 1
- 9
- 15
-
-
1@kpull1 the asker did not restrict the solution domain by asking if a built-in solution exists. – user2910265 Oct 30 '14 at 16:15
-
5Modern note: Moment.js is _incredibly_ heavy to add for such a small purpose. It's several hundred KB, and isn't webpack-friendly out of the box. – Joshua Comeau Nov 25 '16 at 13:24
-
2Our preferred library is date-fns. Webpack-friendly, fast, and treats Dates as immutable. – Freewalker Jun 06 '17 at 20:59
-
1@LukeWilliams never heard of date-fns until now. will check it out. thanks. – user2910265 Jun 06 '17 at 21:23
-
-
@birdus according to Bundle Phobia, it's actually 217kb minified. https://bundlephobia.com/result?p=moment@2.19.2 – Joshua Comeau Nov 14 '17 at 21:11
-
I imagine there are different builds that cut out i18n options you don't need, but the default NPM package is quite large. – Joshua Comeau Nov 14 '17 at 21:11
-
2@JoshuaComeau If you download it from https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js, it takes up 53,248 bytes on disk. I imagine that's the whole ball o' wax, but I don't know. Anyway, whatever. It's not a big deal. – birdus Dec 04 '17 at 18:53
the simplest answer is, assuming the need is to add 1 day to the current date:
var currentDate = new Date();
var numberOfDayToAdd = 1;
currentDate.setDate(currentDate.getDate() + numberOfDayToAdd );
To explain to you, line by line, what this code does:
- Create the current date variable named currentDate. By default "new Date()" automatically assigns the current date to the variable.
- Create a variable to save the number of day(s) to add to the date (you can skip this variable and use directly the value in the third line)
- Change the value of Date (because Date is the number of the month's day saved in the object) by giving the same value + the number you want. The switch to the next month will be automatic

- 499
- 1
- 6
- 23
-
This is my answer. The code is legible and explicit. Not too short and no extraneous parts. The explanation is quite complete. Having a link to the MDN Date page would make it perfect. – ThaJay Jan 31 '23 at 09:48
I created these extensions last night:
you can pass either positive or negative values;
example:
var someDate = new Date();
var expirationDate = someDate.addDays(10);
var previous = someDate.addDays(-5);
Date.prototype.addDays = function (num) {
var value = this.valueOf();
value += 86400000 * num;
return new Date(value);
}
Date.prototype.addSeconds = function (num) {
var value = this.valueOf();
value += 1000 * num;
return new Date(value);
}
Date.prototype.addMinutes = function (num) {
var value = this.valueOf();
value += 60000 * num;
return new Date(value);
}
Date.prototype.addHours = function (num) {
var value = this.valueOf();
value += 3600000 * num;
return new Date(value);
}
Date.prototype.addMonths = function (num) {
var value = new Date(this.valueOf());
var mo = this.getMonth();
var yr = this.getYear();
mo = (mo + num) % 12;
if (0 > mo) {
yr += (this.getMonth() + num - mo - 12) / 12;
mo += 12;
}
else
yr += ((this.getMonth() + num - mo) / 12);
value.setMonth(mo);
value.setYear(yr);
return value;
}

- 243
- 2
- 4
-
7The .addDays() method does not work for dates that cross daylight saving time boundaries. – mskfisher Apr 29 '13 at 00:02
-
2This is one of the better answers here because you (correctly) use the number of millis since the epoc to represent dates/times, and add amounts of millis for adjustments... Why then did you not keep this up for "addMonths"!? And why no add year? Did you get bored? – Robin Apr 23 '15 at 13:03
-
Other than months, time periods can be represented by static numbers. But months can be four different lengths. Also, any time period length from Days and higher may have variable length at DST, so you can't use time-based addition anymore without another level of complexity. I added the addYears(), and I fixed addMonths(). – Suamere Apr 16 '16 at 19:55
A solution designed for the pipeline operator:
const addDays = days => date => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
};
Usage:
// Without the pipeline operator...
addDays(7)(new Date());
// And with the pipeline operator...
new Date() |> addDays(7);
If you need more functionality, I suggest looking into the date-fns library.

- 33,689
- 26
- 132
- 245
Without using the second variable, you can replace 7 with your next x days:
let d=new Date(new Date().getTime() + (7 * 24 * 60 * 60 * 1000));

- 659
- 6
- 10
Short:
function addDays(date, number) {
const newDate = new Date(date);
return new Date(newDate.setDate(newDate.getDate() + number));
}
console.log({
tomorrow: addDays(new Date(), 1)
});
Advance:
function addDays(date, number) {
const newDate = new Date(date);
return new Date(newDate.setDate(date.getDate() + number));
}
function addMonths(date, number) {
const newDate = new Date(date);
return new Date(newDate.setMonth(newDate.getMonth() + number));
}
function addYears(date, number) {
const newDate = new Date(date);
return new Date(newDate.setFullYear(newDate.getFullYear() + number));
}
function getNewDate(dateTime) {
let date = new Date();
let number = parseInt(dateTime.match(/\d+/)[0]);
if (dateTime.indexOf('-') != -1)
number = (-number);
if (dateTime.indexOf('day') != -1)
date = addDays(date, number);
else if (dateTime.indexOf('month') != -1)
date = addMonths(date, number);
else if (dateTime.indexOf('year') != -1)
date = addYears(date, number);
return date;
}
console.log({
tomorrow: getNewDate('+1day'),
yesterday: getNewDate('-1day'),
nextMonth: getNewDate('+1month'),
nextYear: getNewDate('+1year'),
});
With fix provide by jperl

- 730
- 8
- 20
-
2However, this will modify the date object you pass to your functions. If you do this `addDays(today, 1)`, today will now be tomorrow. `function addDays(date, number) { const newDate = new Date(date) return new Date(newDate.setDate(newDate.getDate() + number)); }` – jperl Nov 12 '21 at 18:37
-
2
to substract 30 days use (24h=86400000ms)
new Date(+yourDate - 30 *86400000)
var yourDate=new Date();
var d = new Date(+yourDate - 30 *86400000)
console.log(d)

- 85,173
- 29
- 368
- 345
-
When trying to calculate date time always remember that date time in based on milliseconds in computer science, (https://en.wikipedia.org/wiki/System_time#Retrieving_system_time) new Date(+yourDate + 30 * (24*60*60*1000)) – Ashraf Sada Jan 24 '20 at 16:49
The simplest solution.
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + parseInt(days));
return this;
};
// and then call
var newDate = new Date().addDays(2); //+2 days
console.log(newDate);
// or
var newDate1 = new Date().addDays(-2); //-2 days
console.log(newDate1);

- 4,433
- 2
- 26
- 37

- 145
- 3
- 6
-
I think this is the best solution. If we're extending the Date class, then it makes better sense that the Date instance is itself updated. – Porlune Jun 13 '17 at 08:25
You can try:
var days = 50;
const d = new Date();
d.setDate(d.getDate() + days)
This should work well.

- 2,684
- 2
- 13
- 22

- 177
- 1
- 6
-
3I upvoted this, but the true one liner is new Date( (new Date()).setDate((new Date()).getDate() + 1) ) – ucipass Nov 02 '21 at 20:32
You can use JavaScript, no jQuery required:
var someDate = new Date();
var numberOfDaysToAdd = 6;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
Formatting to dd/mm/yyyy :
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;

- 3,414
- 2
- 21
- 33
-
Would like to vote for this because this answer gives the correct month as date.getMonth() ranges from 0 to 11 and needs to be incremented by 1 as mentioned in this solution. – neonidian May 12 '21 at 09:36
Thanks Jason for your answer that works as expected, here is a mix from your code and the handy format of AnthonyWJones :
Date.prototype.addDays = function(days){
var ms = new Date().getTime() + (86400000 * days);
var added = new Date(ms);
return added;
}

- 1,622
- 18
- 14
-
6It does not take into account daylight saving when there's more or less than 86400000 seconds in a day and can result in logical error (program bug) in your code. – sbrbot Jan 12 '13 at 09:08
-
Sometimes that's needed. The eBay API has x-day auctions which are 24-hour based so your item will end at a different time than it goes up if DST status changes mid-auction. In that case you need to use this type of function to avoid logical errors. – Andy Novocin Jun 23 '14 at 15:17
Late to the party, but if you use there's an excellent plugin called Moment:jQuery
then
var myDateOfNowPlusThreeDays = moment().add(3, "days").toDate();
http://momentjs.com/docs/#/manipulating/
And lots of other good stuff in there!
Edit: jQuery reference removed thanks to aikeru's comment

- 11,639
- 7
- 37
- 56
-
2
-
-
1Yes, after futzing around with Plain ol' JS for too long, I used Moment , and it just works(tm)! – caseyamcl Dec 09 '14 at 13:55
-
-
1@frenchie because, what starts as a few lines of JS and because clearly your application is manipulating days, dates and time related information, it'll soon be a few 1000 lines of JS, then you'll be asked to localise the app across 12 languages and timezones and you'd wished you had started out with something like moment ;) – RemarkLima Mar 05 '17 at 05:08
As simple as this:
new Date((new Date()).getTime() + (60*60*24*1000));

- 10,349
- 9
- 44
- 84

- 1,726
- 21
- 26
-
5While this does add UTC days, it uses local methods and doesn't allow for local days that aren't 24 hours long, which happens when the timezone offset changes (e.g. in and out of daylight saving). – RobG Aug 22 '19 at 10:59
Old I know, but sometimes I like this:
function addDays(days) {
return new Date(Date.now() + 864e5 * days);
}

- 5,314
- 3
- 28
- 45
-
3This one makes the most sense to me. It's simple, elegant and doesn't fall pray to issues moving over months/years. – uadrive May 07 '15 at 04:17
-
1Simplest answer presented. Building on this, the prototype 'addDays' would be the following: `Date.prototype.addDays = function(days) {return new Date(this.getTime() + (864e5 * days));};` – CrazyIvan1974 Jun 03 '15 at 02:11
-
I had issues with daylight savings time with the proposed solution.
By using getUTCDate
/ setUTCDate
instead, I solved my issue.
// Curried, so that I can create helper functions like `add1Day`
const addDays = num => date => {
// Make a working copy so we don't mutate the supplied date.
const d = new Date(date);
d.setUTCDate(d.getUTCDate() + num);
return d;
}

- 2,594
- 24
- 25
Why so complicated?
Let's assume you store the number of days to add in a variable called days_to_add.
Then this short one should do it:
calc_date = new Date(Date.now() +(days_to_add * 86400000));
With Date.now() you get the actual unix timestamp as milliseconds and then you add as many milliseconds as you want to add days to. One day is 24h60min60s*1000ms = 86400000 ms or 864E5.

- 4,237
- 12
- 42
- 47

- 507
- 3
- 24
-
This works! And is actually smart, and short. I tested to see, if you add 30 days or 45 days, and if it calculates correctly and skips the correct months, and seems to be fine. The way this is done is what I was looking for, and using the "out of the box" js date function is the way to go, not complicating it, just using what already exists! Thumbs up! Thanks! – Jess Mar 15 '21 at 10:36
No, javascript has no a built in function, but you can use a simple line of code
timeObject.setDate(timeObject.getDate() + countOfDays);

- 873
- 1
- 10
- 21
The mozilla docs for setDate() don't indicate that it will handle end of month scenarios. See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
setDate()
- Sets the day of the month (1-31) for a specified date according to local time.
That is why I use setTime() when I need to add days.

- 1
- 1

- 824
- 2
- 9
- 15
-
I would link to the ECMAScript docs, but they are released in PDF ;( – Blake Mills Sep 28 '12 at 20:37
-
1Re "*mozilla docs for setDate() don't indicate that it will handle end of month scenarios*". The "docs" have been updated so now they do. ;-) – RobG Dec 06 '16 at 23:15
Generic prototype with no variables, it applies on an existing Date value:
Date.prototype.addDays = function (days) {
return new Date(this.valueOf() + days * 864e5);
}

- 1,423
- 13
- 17
I guess I'll give an answer as well:
Personally, I like to attempt to avoid gratuitous variable declaration, method calls, and constructor calls, as they are all expensive on performance. (within reason, of course)
I was going to leave this as just comment under the Answer given by @AnthonyWJones but thought better of it.
// Prototype usage...
Date.prototype.addDays = Date.prototype.addDays || function( days ) {
return this.setTime( 864E5 * days + this.valueOf() ) && this;
};
// Namespace usage...
namespace.addDaysToDate = function( date, days ) {
return date.setTime( 864E5 * days + date.valueOf() ) && date;
};
// Basic Function declaration...
function addDaysToDate( date, days ) {
return date.setTime( 864E5 * days + date.valueOf() ) && date;
};
The above will respect DST. Meaning if you add a number of days that cross DST, the displayed time (hour) will change to reflect that.
Example:
Nov 2, 2014 02:00 was the end of DST.
var dt = new Date( 2014, 10, 1, 10, 30, 0 );
console.log( dt ); // Sat Nov 01 2014 10:30:00
console.log( dt.addDays( 10 ) ); // Tue Nov 11 2014 09:30:00
If you're looking to retain the time across DST (so 10:30 will still be 10:30)...
// Prototype usage...
Date.prototype.addDays = Date.prototype.addDays || function( days ) {
return this.setDate( this.getDate() + days ) && this;
};
// Namespace usage...
namespace.addDaysToDate = function( date, days ) {
return date.setDate( date.getDate() + days ) && date;
};
// Basic Function declaration...
function addDaysToDate( date, days ) {
return date.setDate( date.getDate() + days ) && date;
};
So, now you have...
var dt = new Date( 2014, 10, 1, 10, 30, 0 );
console.log( dt ); // Sat Nov 01 2014 10:30:00
console.log( dt.addDays( 10 ) ); // Tue Nov 11 2014 10:30:00

- 1,530
- 15
- 20
Extending prototype in javascript may not be a good idea, especially in professional codebases.
What you want to do is extend the native Date
class:
class MyCustomDate extends Date {
addDays(days) {
const date = new MyCustomDate(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
}
const today = new MyCustomDate();
const nextWeek = today.addDays(7)
console.log(nextWeek)
This way, if one day Javascript implements a native addDays
method, you won't break anything.

- 6,919
- 1
- 29
- 36
I use something like:
new Date(dateObject.getTime() + amountOfDays * 24 * 60 * 60 * 1000)
Works with day saving time:
new Date(new Date(2014, 2, 29, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
Works with new year:
new Date(new Date(2014, 11, 31, 20, 0, 0).getTime() + 1 * 24 * 60 * 60 * 1000)
It can be parametrized:
function DateAdd(source, amount, step) {
var factor = 1;
if (step == "day") factor = 24 * 60 * 60 * 1000;
else if (step == "hour") factor = 60 * 60 * 1000;
...
new Date(source.getTime() + amount * factor);
}

- 1,623
- 15
- 19
Edit:
Instead of setTime()
(or setHours()
) you could do it this way:
Date.prototype.addDays= function(d){
this.setDate(this.getDate() + d);
return this;
};
var tomorrow = new Date().addDays(1);
Old:
Instead of using setTime()
you can use setHours()
:
Date.prototype.addDays= function(d){
this.setHours(this.getHours() + d * 24);
return this;
};
var tomorrow = new Date().addDays(1);
See the JSFiddle...

- 701
- 10
- 17
-
following this logic you could also add one day ;) `d.setDate(d.getDate() + 1);` – Rivenfall Feb 24 '16 at 17:43
Very simple code to add days in date in java script.
var d = new Date();
d.setDate(d.getDate() + prompt('how many days you want to add write here'));
alert(d);

- 4,433
- 2
- 26
- 37

- 702
- 2
- 8
- 22
There's a setDate and a getDate method, which allow you to do something like this :
var newDate = aDate.setDate(aDate.getDate() + numberOfDays);
If you want to both subtract a number of days and format your date in a human readable format, you should consider creating a custom DateHelper
object that looks something like this :
var DateHelper = {
addDays : function(aDate, numberOfDays) {
aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays
return aDate; // Return the date
},
format : function format(date) {
return [
("0" + date.getDate()).slice(-2), // Get day and pad it with zeroes
("0" + (date.getMonth()+1)).slice(-2), // Get month and pad it with zeroes
date.getFullYear() // Get full year
].join('/'); // Glue the pieces together
}
}
// With this helper, you can now just use one line of readable code to :
// ---------------------------------------------------------------------
// 1. Get the current date
// 2. Add 20 days
// 3. Format it
// 4. Output it
// ---------------------------------------------------------------------
document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), 20));
(see also this Fiddle)

- 45,213
- 22
- 199
- 169
I am using the following solution.
var msInDay = 86400000;
var daysToAdd = 5;
var now = new Date();
var milliseconds = now.getTime();
var newMillisecods = milliseconds + msInDay * daysToAdd;
var newDate = new Date(newMillisecods);
//or now.setTime(newMillisecods);
Date has a constructor that accepts an int. This argument represents total milliseconds before/after Jan 1, 1970. It also has a method setTime which does the same without creating a new Date object.
What we do here is convert days to milliseconds and add this value to the value provided by getTime. Finally, we give the result to Date(milliseconds) constructor or setTime(milliseconds) method.

- 307
- 5
- 9
-
-
-
`now.setDate(now.getDate() + days);` automatically handles DST changes. And I have to correct, leap seconds are ignored in JS timestamps. – Stephan Aug 25 '17 at 07:57
Our team considers date-fns the best library in this space. It treats dates as immutable (Moment.js will probably never adopt immutability), it's faster, and can be loaded modularly.
const newDate = DateFns.addDays(oldDate, 2);

- 6,329
- 4
- 51
- 70
There is a problem with this kind of functions, I solve it with parseInt()
Date.prototype.addDays = function(dias) {
var date = new Date(this.valueOf());
date.setDate(parseInt(date.getDate()) + parseInt(dias));
return date;
}
Date.prototype.addMonths = function(months) {
var date = new Date(this.valueOf());
date.setMonth(parseInt(date.getMonth()) + parseInt(months));
return date;
}
Date.prototype.addYears = function(years) {
var date = new Date(this.valueOf());
date.setFullYear(parseInt(date.getFullYear()) + parseInt(years));
return date;
}

- 387
- 3
- 5
//the_day is 2013-12-31
var the_day = Date.UTC(2013, 11, 31);
// Now, the_day will be "1388448000000" in UTC+8;
var the_next_day = new Date(the_day + 24 * 60 * 60 * 1000);
// Now, the_next_day will be "Wed Jan 01 2014 08:00:00 GMT+0800"

- 489
- 1
- 3
- 4
-
I believe this wouldn't work with daylight saving times, leap seconds, or other timezone changes causing a day to not have 86400s. – Nicolas Cortot Dec 28 '13 at 16:29
-
This is the one that is correct. Date.UTC makes the difference. The only thing to that needs caution is that month starts from 0. – Devid Jan 24 '17 at 16:03
For those using Angular:
Just do:
$scope.booking.totTijd.setMinutes($scope.booking.totTijd.getMinutes()+15);
$scope.booking.totTijd.setDate($scope.booking.totTijd.getDate() + 1);

- 1,428
- 1
- 17
- 25
You can create your custom helper function here
function plusToDate(currentDate, unit, howMuch) {
var config = {
second: 1000, // 1000 miliseconds
minute: 60000,
hour: 3600000,
day: 86400000,
week: 604800000,
month: 2592000000, // Assuming 30 days in a month
year: 31536000000 // Assuming 365 days in year
};
var now = new Date(currentDate);
return new Date(now + config[unit] * howMuch);
}
var today = new Date();
var theDayAfterTommorow = plusToDate(today, 'day', 2);
By the way, this is generic solution for adding seconds or minutes or days whatever you want.

- 1,269
- 1
- 11
- 25
-
Even assuming that a day has 86400 seconds can be incorrect, assuming that there are no leap years can lead to serious errors. – Stephan Aug 24 '17 at 12:46
-
...And also in case of daylight saving settings, difference between two day is not always 24h. – Tobia Jan 20 '18 at 07:10
The easiest way to get this done is using date-fns library.
var addDays = require('date-fns/add_days')
addDays(date, amount)
The documentation is available in this link here. You can also get this done using moment.js. The reference link is here
Hope it helps!

- 635
- 1
- 8
- 15
-
There are already answers with hundreds of votes showing how to do this in one line with the native API. Why on earth would you suggest a whole library? – havlock Jun 24 '18 at 15:39
-
I actually wanted to expose these libraries that can perform such tasks with ease. Also I've mentioned the reference links of 2 such libraries so that anyone who looks into this may want to manipulate Date objects in other ways. I thought of contributing this issue in a different way. – Tuan Jun 25 '18 at 16:10
I've used this approach to get the right date in one line to get the time plus one day following what people were saying above.
((new Date()).setDate((new Date()).getDate()+1))
I just figured I would build off a normal (new Date())
:
(new Date()).getDate()
> 21
Using the code above I can now set all of that within Date()
in (new Date())
and it behaves normally.
(new Date(((new Date()).setDate((new Date()).getDate()+1)))).getDate()
> 22
or to get the Date
object:
(new Date(((new Date()).setDate((new Date()).getDate()+1))))

- 729
- 7
- 10
I can't believe there's no cut'n'paste solution in this thread after 5 years!
SO: To get the same time-of-day regardless of summertime interference:
Date.prototype.addDays = function(days)
{
var dat = new Date( this.valueOf() )
var hour1 = dat.getHours()
dat.setTime( dat.getTime() + days * 86400000) // 24*60*60*1000 = 24 hours
var hour2 = dat.getHours()
if (hour1 != hour2) // summertime occured +/- a WHOLE number of hours thank god!
dat.setTime( dat.getTime() + (hour1 - hour2) * 3600000) // 60*60*1000 = 1 hour
return dat
or
this.setTime( dat.getTime() ) // to modify the object directly
}
There. Done!

- 4,245
- 3
- 23
- 25
-
You should really avoid assigning to `prototype` of built-ins, as this is considered a bad practice and can lead to bugs. Consider using a function or a wrapper class around a built-in instead. – Vasiliy Artamonov Nov 19 '22 at 19:42
function addDays(n){
var t = new Date();
t.setDate(t.getDate() + n);
var month = "0"+(t.getMonth()+1);
var date = "0"+t.getDate();
month = month.slice(-2);
date = date.slice(-2);
var date = date +"/"+month +"/"+t.getFullYear();
alert(date);
}
addDays(5);

- 2,976
- 23
- 30
Some implementations to extend Date https://gist.github.com/netstart/c92e09730f3675ba8fb33be48520a86d
/**
* just import, like
*
* import './../shared/utils/date.prototype.extendions.ts';
*/
declare global {
interface Date {
addDays(days: number, useThis?: boolean): Date;
addSeconds(seconds: number): Date;
addMinutes(minutes: number): Date;
addHours(hours: number): Date;
addMonths(months: number): Date;
isToday(): boolean;
clone(): Date;
isAnotherMonth(date: Date): boolean;
isWeekend(): boolean;
isSameDate(date: Date): boolean;
getStringDate(): string;
}
}
Date.prototype.addDays = function(days: number): Date {
if (!days) {
return this;
}
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.addSeconds = function(seconds: number) {
let value = this.valueOf();
value += 1000 * seconds;
return new Date(value);
};
Date.prototype.addMinutes = function(minutes: number) {
let value = this.valueOf();
value += 60000 * minutes;
return new Date(value);
};
Date.prototype.addHours = function(hours: number) {
let value = this.valueOf();
value += 3600000 * hours;
return new Date(value);
};
Date.prototype.addMonths = function(months: number) {
const value = new Date(this.valueOf());
let mo = this.getMonth();
let yr = this.getYear();
mo = (mo + months) % 12;
if (0 > mo) {
yr += (this.getMonth() + months - mo - 12) / 12;
mo += 12;
} else {
yr += ((this.getMonth() + months - mo) / 12);
}
value.setMonth(mo);
value.setFullYear(yr);
return value;
};
Date.prototype.isToday = function(): boolean {
const today = new Date();
return this.isSameDate(today);
};
Date.prototype.clone = function(): Date {
return new Date(+this);
};
Date.prototype.isAnotherMonth = function(date: Date): boolean {
return date && this.getMonth() !== date.getMonth();
};
Date.prototype.isWeekend = function(): boolean {
return this.getDay() === 0 || this.getDay() === 6;
};
Date.prototype.isSameDate = function(date: Date): boolean {
return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
};
Date.prototype.getStringDate = function(): string {
// Month names in Brazilian Portuguese
const monthNames = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
// Month names in English
// let monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const today = new Date();
if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay()) {
return 'Hoje';
// return "Today";
} else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() + 1) {
return 'Amanhã';
// return "Tomorrow";
} else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() - 1) {
return 'Ontem';
// return "Yesterday";
} else {
return this.getDay() + ' de ' + this.monthNames[this.getMonth()] + ' de ' + this.getFullYear();
// return this.monthNames[this.getMonth()] + ' ' + this.getDay() + ', ' + this.getFullYear();
}
};
export {};

- 1,292
- 12
- 15
date d = new Date() // current date
date tomorrow = d.setMonth(d.getMonth(),d.getDate()+1) // return a date incremented by 0 months and 1 day

- 1,121
- 12
- 20
-
I changed the line after I realized that with 0 was creating the a date starting from January – Salvatore Pannozzo Capodiferro Sep 23 '22 at 12:38
new Date(Date.now() + 2000 * 86400)
This snippet adds two days to the current date using the "2000" argument. You can tweak the number of days by updating the "2000" value in the second argument.
You can use this single line format to add days to the current date using the native JavaScript date.

- 39
- 8
2.39KB minified. One file. https://github.com/rhroyston/clock-js
console.log(clock.what.weekday(clock.now + clock.unit.days)); //"wednesday"
console.log(clock.what.weekday(clock.now + (clock.unit.days * 2))); //"thursday"
console.log(clock.what.weekday(clock.now + (clock.unit.days * 3))); //"friday"
<script src="https://raw.githubusercontent.com/rhroyston/clock-js/master/clock.min.js"></script>

- 4,433
- 2
- 26
- 37

- 16,778
- 6
- 77
- 91
try this
function addDays(date,days) {
var one_day=1000*60*60*24;
return new Date(date.getTime()+(days*one_day)).toLocaleDateString();
}

- 35
- 1
-
4Don't use this, in case of daylight saving setting this doesn't work because day difference is not 24h. I did the same mistake... – Tobia Jan 20 '18 at 07:09
Use js-joda. It is an awesome immutable date and time library for javascript. Here is an excerpt from its cheat-sheet.
Add 17 days to today
LocalDate.now().plusDays(17);
You can also build the desired date from now multiple operations at once.
LocalDate.now()
.plusMonths(1)
.withDayOfMonth(1)
.minusDays(17);
Or:
var d = LocalDate.parse('2019-02-23');
d.minus(Period.ofMonths(3).plusDays(3)); // '2018-11-20'

- 2,188
- 2
- 28
- 23
My test exemple can do adition an minus in the same instance of Date Object.
Date.prototype.reset = function()
{
let newDate = new Date(this.timeStamp)
this.setFullYear (newDate.getFullYear())
this.setMonth (newDate.getMonth())
this.setDate (newDate.getDate())
this.setHours (newDate.getHours())
this.setMinutes (newDate.getMinutes())
this.setSeconds (newDate.getSeconds())
this.setMilliseconds (newDate.getMilliseconds())
}
Date.prototype.addDays = function(days)
{
this.timeStamp = this[Symbol.toPrimitive]('number')
let daysInMiliseconds = (days * (1000 * 60 * 60 * 24))
this.timeStamp = this.timeStamp + daysInMiliseconds
this.reset()
}
Date.prototype.minusDays = function(days)
{
this.timeStamp = this[Symbol.toPrimitive]('number')
let daysInMiliseconds = (days * (1000 * 60 * 60 * 24))
if(daysInMiliseconds <= this.timeStamp)
{
this.timeStamp = this.timeStamp - daysInMiliseconds
this.reset()
}
}
var temp = new Date(Date.now())// from now time
console.log(temp.toDateString())
temp.addDays(31)
console.log(temp.toDateString())
temp.minusDays(5)
console.log(temp.toDateString())

- 21
- 2
I sum hours and days...
Date.prototype.addDays = function(days){
days = parseInt(days, 10)
this.setDate(this.getUTCDate() + days);
return this;
}
Date.prototype.addHours = function(hrs){
var hr = this.getUTCHours() + parseInt(hrs , 10);
while(hr > 24){
hr = hr - 24;
this.addDays(1);
}
this.setHours(hr);
return this;
}

- 69
- 1
- 2
For everybody who don't know how to make it work : there is a full working code it's not perfect but you can copy past it and it's working.
In InDesign creat a .jsx
in the startup scripts folder in "Program Files\Adobe\Adobe InDesign 2021\Scripts\startup scripts"
.
You can us the Extendscript Toolkit CC in the creative cloud to make it and paste this:
The restart indesign and jjmmyyyy
+30 should be in the texte variable.
this will show the date like this jj/m/yyyy
idk how to make it show 24/07/2021
insted of 24/7/2021
but goodenough for me .
#targetengine 'usernameVariable'
function addVariables(openEvent)
{
var doc = openEvent.parent;
while ( doc.constructor.name != "Document" )
{
if ( doc.constructor.name == "Application" ){ return; }
doc = doc.parent;
}
// from http://stackoverflow.com/questions/563406/add-days-to-datetime
var someDate = new Date();
var numberOfDaysToAdd = 30;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;
createTextVariable(doc, "jjmmyyyy+30", someFormattedDate);
}
function createTextVariable(target, variableName, variableContents)
{
var usernameVariable = target.textVariables.itemByName(variableName);
if (!usernameVariable.isValid)
{
usernameVariable = target.textVariables.add();
usernameVariable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;
usernameVariable.name = variableName;
}
usernameVariable.variableOptions.contents = variableContents;
}
app.addEventListener('afterOpen', addVariables);

- 5,578
- 4
- 21
- 35

- 15
- 4
the same answer: How to add number of days to today's date?
function DaysOfMonth(nYear, nMonth) {
switch (nMonth) {
case 0: // January
return 31; break;
case 1: // February
if ((nYear % 4) == 0) {
return 29;
}
else {
return 28;
};
break;
case 2: // March
return 31; break;
case 3: // April
return 30; break;
case 4: // May
return 31; break;
case 5: // June
return 30; break;
case 6: // July
return 31; break;
case 7: // August
return 31; break;
case 8: // September
return 30; break;
case 9: // October
return 31; break;
case 10: // November
return 30; break;
case 11: // December
return 31; break;
}
};
function SkipDate(dDate, skipDays) {
var nYear = dDate.getFullYear();
var nMonth = dDate.getMonth();
var nDate = dDate.getDate();
var remainDays = skipDays;
var dRunDate = dDate;
while (remainDays > 0) {
remainDays_month = DaysOfMonth(nYear, nMonth) - nDate;
if (remainDays > remainDays_month) {
remainDays = remainDays - remainDays_month - 1;
nDate = 1;
if (nMonth < 11) { nMonth = nMonth + 1; }
else {
nMonth = 0;
nYear = nYear + 1;
};
}
else {
nDate = nDate + remainDays;
remainDays = 0;
};
dRunDate = Date(nYear, nMonth, nDate);
}
return new Date(nYear, nMonth, nDate);
};

- 1
- 1

- 17
- 1
-
4You'll need to update your leap year check - if the year is divisible by 100 and not by 400, it's not a leap year (2000 was; 2100 won't be) – andrewsi Oct 05 '12 at 16:38