I need to set a date that would be 30 days from now taking into account months that are 28,29,30,31 days so it doesn't skip any days and shows exactly 30 days from now. How can I do that?
Asked
Active
Viewed 4.8k times
13
-
1See [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) – Madara's Ghost Oct 26 '11 at 19:55
-
If someone end up here in the future. https://stackoverflow.com/questions/5511323/calculate-the-date-yesterday-in-javascript/58798740#58798740 Provides a one liner – Griffin Mar 13 '20 at 20:35
8 Answers
46
The JavaScript "Date()" object has got you covered:
var future = new Date();
future.setDate(future.getDate() + 30);
That'll just do the right thing. (It's a little confusing that the getter/setters for day-of-month have the names they do.)

Pointy
- 405,095
- 59
- 585
- 614
-
1I don't think this works, at least according to the docs: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/setDate the parameter to `setDate` should be between 1 and 31. However, I kind of remember using this once and it working, so... maybe it does... – Domenic Oct 26 '11 at 19:58
-
Adding `new Date(future.setDate(future.getDate() + 30)).toString();` works for me. – user229044 Oct 26 '11 at 20:04
-
It works as described, but you have to use native JavaScript (var future) variable. Works for me as expected. – Mindaugas Jaraminas May 08 '13 at 11:56
-
This works for me, but get/setDate refer to the day of the month, which seems unnecessarily confusing (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate). For an alternative, see my answer. – Rich Apodaca Jun 23 '13 at 19:38
-
It works,it returns date in Milliseconds. newDate(future.setDate(future.getDate() + 30)) return in proper d/m/y format..Thanks – Sajin M Aboobakkar Sep 11 '14 at 10:29
7
Using the native Date object with straightforward syntax and no external libraries:
var future = new Date('Jan 1, 2014');
future.setTime(future.getTime() + 30 * 24 * 60 * 60 * 1000); // Jan 31, 2014
The Date setTime and getTime functions use milliseconds since Jan 1, 1970 (link).

Rich Apodaca
- 28,316
- 16
- 103
- 129
7
I wrote a Date wrapper library that helps with parsing, manipulating, and formatting dates.
https://github.com/timrwood/moment
Here is how you would do it with Moment.js
var inThirtyDays = moment().add('days', 30);

timrwood
- 10,611
- 5
- 35
- 42
-
the [documentation](https://momentjs.com/docs/#/manipulating/add/) says `moment().add(30, 'days');` – suhailvs Mar 09 '20 at 09:54
-
Note from the authors of moment.js: "_we would like to discourage Moment from being used in new projects going forward_" ([source](https://momentjs.com/docs/#/-project-status/)). – starball Aug 11 '23 at 23:39
4
var now = new Date();
var THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
var thirtyDaysFromNow = now + THIRTY_DAYS;

Domenic
- 110,262
- 41
- 219
- 271
-
Doesn't work for me; `thirtyDaysFromNow.toString()` yields "Invalid Date". `new Date(now +
)` either yields "Invalid Date" or the same value as `now`. I don't think this does what you think it does. – user229044 Oct 26 '11 at 20:00 -
3This will work, but instead use `var now = new Date().getTime();` – John Strickler Oct 26 '11 at 20:05
-
1
Try this piece of code:
const date = new Date();
futureDate = new Date(date.setDate(date.getDate() + 30)).toLocaleDateString();

nurealam siddiq
- 1,567
- 10
- 9
0
const today = new Date();
const thirty_days_from_now = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000),

zemunkh
- 662
- 7
- 13
0
const afterMonth = new Date(new Date().setDate(new Date().getDate()+30)).toISOString() const Now = new Date().toISOString()

Ibra Him
- 1
-1
I've been able to make this work:
function() {
// Get local time as ISO string with offset at the end
var now = new Date();
now.setMonth(now.getMonth() + 1);
var pad = function(num) {
var norm = Math.abs(Math.floor(num));
return (norm < 10 ? '0' : '') + norm;
};
return now.getFullYear()
+ '-' + pad(now.getMonth()+1)
+ '-' + pad(now.getDate());
}
-
This doesn’t seem to handle the cases where the current month is not exactly 30 days. For example, what does this give when today is February 1, 2020? – NicholasM Nov 14 '19 at 03:17