I need to set a date that will be in 30 days taking into account the months, which are 28,29,30,31 days, so it does not miss any days and shows exactly after 30 days. How can i do this?
The JavaScript Date () object covers you:
var future = new Date(); future.setDate(future.getDate() + 30);
It will be right. (Itβs a little embarrassing that the recipient / setter for the day of the month has names that they make.)
I wrote a date wrapper library that helps with parsing, manipulating, and formatting dates.
https://github.com/timrwood/moment
Here is how you could do it with Moment.js
var inThirtyDays = moment().add('days', 30);
Date :
var future = new Date('Jan 1, 2014'); future.setTime(future.getTime() + 30 * 24 * 60 * 60 * 1000); // Jan 31, 2014
Date functions setTime and getTime use milliseconds since January 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();