Add Month(s) to a Date in JavaScript or Node.js

Here at Future Studio, we offer a selection of premium tutorials that require payment. You need to enroll for a semester in the Future Studio University.

The guest semester is an enrollment on a monthly basis. In case you’re enrolling now in the University, the next payment date is in one month. We also offer a semester enrollment which is valid for six months.

This tutorial shows you how to add one or more months to a date in JavaScript.

Node.js Series Overview

  1. How to Check if an Object is Empty in JavaScript or Node.js (Coming soon)

Add Month(s) to a Date in JavaScript

JavaScript comes with a Date class providing useful date handling. You can customize a date instance by adding the units you need. A date instance comes with .setX methods to customize it.

In this case, you want to add a given number of months to a date. Add the given amount of months to a date instance using date.setMonth(currentMonth + delay).

/**
 * Returns the date with added `months` of delay.
 *
 * @param {Number} months - the delay in months
 *
 * @returns {Date}
 */
function dateWithMonthsDelay (months) {  
  const date = new Date()
  date.setMonth(date.getMonth() + months)

  return date
}

Notice that date.setMonth directly changes the date instance.

A nice benefit of using this built-in method is that JavaScript takes care of switching the unit. For example, JavaScript also adjusts the years of your date instance if adding the months would exceed December. Sweet!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.