JavaScript Add Month to Date

Jul 7, 2023

JavaScript Dates have a setMonth() function that sets the current month. You can use setMonth() with getMonth() to increment your date's current month by 1 as follows:

const date = new Date('2022-06-04');
date.setMonth(date.getMonth() + 1);

date.toISOString(); // '2022-07-04T00:00:00.000Z'

setMonth() will keep the current day of the month, so date.setMonth(date.getMonth() + 1) will skip to the same day next month. setMonth() also takes into account months with different amounts of days, and rolling over years.

const shortMonth = new Date('2022-02-04');
shortMonth.setMonth(shortMonth.getMonth() + 1);
shortMonth.toISOString(); // '2022-03-04T00:00:00.000Z'

const rolloverYear = new Date('2022-12-06');
rolloverYear.setMonth(rolloverYear.getMonth() + 1);
rolloverYear.toISOString(); // '2023-01-06T00:00:00.000Z'

Remember that months are 0-indexed in JavaScript dates; getMonth() returns 0 for January, 1 for February, etc.

const rolloverYear = new Date('2022-12-06');
rolloverYear.getMonth(); // 11

While we recommend using vanilla JavaScript dates for simple operations like adding a month, we understand that many developers use date helper libraries. With that in mind, here's examples using date-fns and Luxon.

Using date-fns

Date-fns has a neat add() function that lets you add months to the current date.

const add = require('date-fns/add');

// Returns a date instance representing July 4, 2022 UTC time.
add(new Date('2022-06-04'), { months: 1 });

Using Luxon

Luxon's plus() method is roughly equivalent to date-fns' add(). You can use plus() to add months to a Luxon DateTime as follows.

const { DateTime } = require('luxon');

// Returns a date instance representing July 4, 2022 UTC time.
DateTime.fromJSDate(new Date('2022-06-04')).plus({ months: 1 }).toJSDate();

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!