Javascript - Set date 30 days from now

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).


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);

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.)


var now = new Date();
var THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000;
var thirtyDaysFromNow = now + THIRTY_DAYS;