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?
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.)
I wrote a Date wrapper library that helps with parsing, manipulating, and formatting dates.
Here is how you would do it with Moment.js
var inThirtyDays = moment().add('days', 30);
Add 30 days to a Current date - JS, Definition and Usage. The setDate() method sets the day of the month to the date object. If the month has 30 days: 32 will result in the second day of the next� A simple way to get it done is to send the timestamp value in the Date constructor. To calculate 30 days measured in timestamp: 30 * 24 * 60 * 60 * 1000. Then, you need the current timestamp: Date.now() Finally, sum both values and send the result as a param in the constructor: var nowPlus30Days = new Date(Date.now() + (30 * 24 * 60 * 60 * 1000));
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).
var now = new Date(); var THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000; var thirtyDaysFromNow = now + THIRTY_DAYS;
Try this piece of code:
const date = new Date(); futureDate = new Date(date.setDate(date.getDate() + 30)).toLocaleDateString();
Date.prototype.setDate(), getDate()-30) Output=1511308800000 2nd scenario:- if i put the code as var date ="12/22/2017" var now=new Date(date); now.setDate(now. Date Methods. When a Date object is created, a number of methods allow you to operate on it.. Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.