Last updated: Mar 6, 2024
Reading timeยท4 min
To add months to a date:
getMonth()
method to get a zero-based value for the month of the
given date.setMonth()
method to set the month for the date.setMonth
method takes a zero-based integer for the month and sets the
value for the date.function addMonths(date, months) { date.setMonth(date.getMonth() + months); return date; } // โ Add 2 months to the current Date const result1 = addMonths(new Date(), 2); console.log(result1); // ๐๏ธ 2023-09-27T07:17:09.816Z // ---------------------------------------------- // โ Add 4 months to a different date const date = new Date('2023-04-14T00:00:00.000Z'); const result2 = addMonths(date, 4); console.log(result2); // ๐๏ธ 2023-08-14T00:00:00.000Z
The addMonths
function takes a Date
object and N as parameters and adds N
months to the date.
If you need to add months to the current date, call the Date()
constructor
without passing it any arguments.
function addMonths(date, months) { date.setMonth(date.getMonth() + months); return date; } // โ Add 2 months to the current Date const currentDate = new Date(); const result1 = addMonths(currentDate, 2); console.log(result1); // ๐๏ธ 2023-09-27T07:18:31.607Z
The Date.getMonth() method
returns an integer between 0
(January) and 11
(December), representing the
month of the given date.
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.
function addMonths(date, months) { date.setMonth(date.getMonth() + months); return date; } // โ Add 13 months to a date const date = new Date('2023-04-14T00:00:00.000Z'); const result = addMonths(date, 13); console.log(result); // ๐๏ธ 2024-05-14T00:00:00.000Z
We added 13
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.
function addMonths(date, months) { const dateCopy = new Date(date); dateCopy.setMonth(dateCopy.getMonth() + months); return dateCopy; } const date = new Date('2023-04-14T00:00:00.000Z'); const result = addMonths(date, 5); console.log(result); // ๐๏ธ 2023-09-14T00:00:00.000Z console.log(date); // ๐๏ธ 2023-04-14T00:00:00.000Z
When a Date
object is passed to the Date()
constructor, it gets converted to
a timestamp and can be used to create a copy of the date.
Mutating function parameters is a bad practice because calling the function with the same parameter multiple times returns different results.
Instead, pure functions like the one above return the same output when called with the same parameters.
You might see the setMonth
method used with 2
parameters. The parameters the
method takes are:
month
- a zero-based (0 = January, 1 = February, etc) value for the month
of the year.day of the 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.
You can also use date-fns module to add months to a date.
import {addMonths} from 'date-fns'; const date = new Date('2023-04-14T00:00:00.000Z'); const result1 = addMonths(date, 1); console.log(result1); // ๐๏ธ 2023-05-14T00:00:00.000Z const result2 = addMonths(date, 2); console.log(result2); // ๐๏ธ 2023-06-14T00:00:00.000Z
The addMonths()
function takes a date and the number of months to be added to
the date as parameters.
The function doesn't mutate the original date as shown in the example.
If you don't have date-fns
installed, you can install it by running the
following command from your terminal.
# ๐๏ธ create package.json if you don't have one npm init -y # โ install with NPM npm install date-fns # โ install with YARN yarn add date-fns
You can also use the moment.js module to add months to a date.
import moment from 'moment'; const date = new Date('2023-04-14T00:00:00.000Z'); const result1 = moment(date).add(1, 'months'); console.log(result1); // ๐๏ธ 2023-05-14T00:00:00.000Z const result2 = moment(date).add(2, 'months'); console.log(result2); // ๐๏ธ 2023-06-14T00:00:00.000Z console.log(date); // ๐๏ธ 2023-04-14T00:00:00.000Z
We used the moment().add()
method to add months to a date.
The method can be used to add years, quarters, months, weeks, days, hours, minutes, seconds or milliseconds to a date.
If you don't have moment
installed, you can install it by running the
following command from your terminal.
# ๐๏ธ create package.json if you don't have one npm init -y # โ install with NPM npm install moment # โ install with YARN yarn add moment
The call to the add()
method actually returns a moment
object and not a
native JavaScript date.
If you need to convert the value to a JavaScript date, use the toDate()
method.
import moment from 'moment'; const date = new Date('2023-04-14T00:00:00.000Z'); const result1 = moment(date).add(1, 'months').toDate(); console.log(result1); // ๐๏ธ 2023-05-14T00:00:00.000Z const result2 = moment(date).add(2, 'months').toDate(); console.log(result2); // ๐๏ธ 2023-06-14T00:00:00.000Z console.log(date); // ๐๏ธ 2023-04-14T00:00:00.000Z
The toDate()
method takes care of converting the moment
object to a native
JavaScript Date object.
I've also written an article on how to subtract months from a Date.
You can learn more about the related topics by checking out the following tutorials: