Adding Months to a Date in JavaScript

How to add months to a date in JavaScript?

Corrected as of 25.06.2019:

var newDate = new Date(date.setMonth(date.getMonth()+8));

Old
From here:

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009 = jan312009.setMonth(jan312009.getMonth()+8);

JavaScript function to add X months to a date

The following function adds months to a date in JavaScript (source). It takes into account year roll-overs and varying month lengths:





function addMonths(date, months) {

var d = date.getDate();

date.setMonth(date.getMonth() + +months);

if (date.getDate() != d) {

date.setDate(0);

}

return date;

}


// Add 12 months to 29 Feb 2016 -> 28 Feb 2017

console.log(addMonths(new Date(2016,1,29),12).toString());


// Subtract 1 month from 1 Jan 2017 -> 1 Dec 2016

console.log(addMonths(new Date(2017,0,1),-1).toString());


// Subtract 2 months from 31 Jan 2017 -> 30 Nov 2016

console.log(addMonths(new Date(2017,0,31),-2).toString());


// Add 2 months to 31 Dec 2016 -> 28 Feb 2017

console.log(addMonths(new Date(2016,11,31),2).toString());

Add months to a date is not working in JavaScript

You should place new Date(addDate).getMonth() + 1 in parentheses. You are building a string and since you don't provide explicit precedence, first new Date(addDate).getMonth() gets added, then 1 gets concatenated to the string.

Try this:





var startDateFormat = new Date(2018, 11, 24);


var addDate = startDateFormat.setMonth(startDateFormat.getMonth() + 45);


console.log(new Date(addDate).getFullYear() + "-" + (new Date(addDate).getMonth() + 1) + "-" + new Date(addDate).getUTCDate())

Adding months to date in js

By calling start.setMonth you end up updating the month on both dates. (This is noted in one of the comments on the answer you followed, but not in the answer itself.)

Separate the statements to only affect the date you want changed:





let start = new Date();
let end = new Date(start);
end.setMonth(end.getMonth() + 1);

console.log(start, end)

I want to add months into a date and get in mm/dd/yy format

First you'll need to get your date into a Date object.

var myDate = new Date("2016-04-01 00:00:00");

Date needs the input to be formatted in the ISO format.

You can then use the following to add different numbers of months:

var myDate_6Months = new Date(myDate.setMonth(myDate.getMonth() + 6));

The line above creates a new Date object by grabbing your original date's month, and adding a desired amount of months to it.

If you were to output myDate_6Months you would get:

Sat Oct 01 2016 00:00:00

To add 11 months:

var myDate_11Months = new Date(myDate.setMonth(myDate.getMonth() + 11))

which would result in:

Wed Mar 01 2017 00:00:00

In order to get the date back in the mm/dd/yy one easy method is to use moment.js.

moment(myDate_6Months).format('MM/DD/YY')

Adding months to a Date in JavaScript

You need to get business rules for adding months. The simple solution is:

function addMonths(dateObj, num) {
return dateObj.setMonth(dateObj.getMonth() + num);
}

However, that will change 31 July to 31 September, which will be converted to 1 October. Also, 31 January plus 1 month is 31 February which will be converted to 2 or 3 March depending on whether it's a leap year or not.

You might expect the first to be 30 September and the second to be 28 or 29 February (depending on whether it's a leap year or not).

So if you want "end of months" be observed, you need to do something like:

function addMonths(dateObj, num) {

var currentMonth = dateObj.getMonth() + dateObj.getFullYear() * 12;
dateObj.setMonth(dateObj.getMonth() + num);
var diff = dateObj.getMonth() + dateObj.getFullYear() * 12 - currentMonth;

// If don't get the right number, set date to
// last day of previous month
if (diff != num) {
dateObj.setDate(0);
}
return dateObj;
}

But check with whoever is responsible for the business rules that that is what they want.

Edit

The above works well, but in response to McShaman's comment here is a version with a simpler check for the month roll–over:





function addMonths(date, months) {

var d = date.getDate();

date.setMonth(date.getMonth() + +months);

if (date.getDate() != d) {

date.setDate(0);

}

return date;

}


// Add 12 months to 29 Feb 2016 -> 28 Feb 2017

console.log(addMonths(new Date(2016,1,29),12).toString());


// Subtract 1 month from 1 Jan 2017 -> 1 Dec 2016

console.log(addMonths(new Date(2017,0,1),-1).toString());


// Subtract 2 months from 31 Jan 2017 -> 30 Nov 2016

console.log(addMonths(new Date(2017,0,31),-2).toString());


// Add 2 months to 31 Dec 2016 -> 28 Feb 2017

console.log(addMonths(new Date(2016,11,31),2).toString());

How do I calculate the date in JavaScript three months prior to today?


var d = new Date();
d.setMonth(d.getMonth() - 3);

This works for January. Run this snippet: