Add Months to a Date in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Add Months to a Date in JavaScript #

To add months to a date:

  1. Use the getMonth() method to get a zero-based value for the month of the given date.
  2. Use the setMonth() method to set the month for the date.
  3. The setMonth method takes a zero-based integer for the month and sets the value for the date.
index.js
function addMonths(numOfMonths, date = new Date()) { date.setMonth(date.getMonth() + numOfMonths); return date; } // 👇️ Add 2 months to current Date const result = addMonths(2); // 👇️ Add months to another date const date = new Date('2022-02-24'); console.log(addMonths(3, date)); // 👉️ Tue May 24 2022

We created a reusable function that takes the number of months and a Date object and adds the months to the date.

If no Date object is provided to the function, it uses the current date.

The getMonth() method returns an integer between 0 (January) and 11 (December), representing the month of the given date.

Note that the value is zero-based, e.g. January = 0, February = 1, March = 2, etc.

The setMonth() method takes a zero-based value representing the month of the year (0 = January, 1 = February, etc.) and sets the value for the date.

The JavaScript Date object automatically takes care of rolling over the year if adding X months to a date pushes us into the next year.

index.js
const date = new Date('2022-12-24'); date.setMonth(date.getMonth() + 3); console.log(date); // 👉️ Fri Mar 24 2023 (year adjusted)

We added 3 months to the date, which pushed us into the next year and the Date object automatically took care of updating the year.

Note that the setMonth method mutates the Date object it was called on. If you don't want to change the Date in place, you can create a copy of it before calling the method.
index.js
function addMonths(numOfMonths, date = new Date()) { const dateCopy = new Date(date.getTime()); dateCopy.setMonth(dateCopy.getMonth() + numOfMonths); return dateCopy; } const date = new Date('2022-02-24'); const result = addMonths(1, date); console.log(result); // 👉️ Thu Mar 24 2022 console.log(date); // 👉️ Thu Feb 24 2022 (didn't change original)

The getTime method returns the number of milliseconds elapsed between 1st of January, 1970 00:00:00 and the given date.

We used the timestamp to create a copy of the Date object, so we don't mutate it in place when calling the setMonth method.

Copying the date is quite useful when you have to use the original Date object in other places in your code.

In general, mutating function arguments is a bad practice, as passing the same parameter to the same function multiple times produces different results.

You might see the setMonth method used with 2 parameters. The parameters the method takes are:

  1. month - a zero-based (0 = January, 1 = February, etc) value for the month of the year.
  2. day of month (optional) - an integer from 1 to 31 that represents the day of the month.

The day of the month parameter is optional, and when not specified, the value returned from the getDate() method is used.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.