JavaScript: Add Month to a Date

Date() constructor creates different date values. While working with dates in JavaScript, we often need to change the dates, like adding months to date before processing it. This article will discuss different ways to add months to a javascript date.

Add Months to a Date Value by Accessing Properties of a Date

Javascript’s getFullYear() will return the year of the date specified according to the local time.

Javascript’s getMonth() will return the month of the date specified according to the local time. The month value is zero-based that is zero indicates the first month of the year.

Javascript’s getDate() will return the day of the month of the date specified according to the local time.

Example 1:-

Add two months to today’s date

Code:-

// function to add no of months to a date
function addMonthsToDate(_date,_noOfMonths) 
 {
        var yearFromDate = _date.getFullYear();
        var monthFromYear = _date.getMonth();
        var dayFromYear = _date.getDate();
        var newDate = new Date(yearFromDate, monthFromYear  + _noOfMonths, dayFromYear);
        return newDate;
 }   
//usage of the function addMonthsToDate
var todayDate = new Date();
console.log("today's date: " + todayDate )
console.log("New Date: " + addMonthsToDate(todayDate,2));

Output:-

Today's Date: Thu Feb 24 2022 23:21:10 GMT+0530 (India Standard Time)
New Date: Sun Apr 24 2022 00:00:00 GMT+0530 (India Standard Time)

Explanation:-

  • Here, we first get today’s date from the system using the new Date().
  • Then we access different properties of the date.
  • getFullYear() method gets the year from today’s date.
  • getMonth() method receives the current month from today’s date.
  • getDate() method receives the day from today’s date.
  • After accessing all the above values, create a new date by Date() constructor, which sets the value of day and year based on how many months we are adding to the current month.

Example2:-

Add 13 months to today’s date

Code:-

//function to add months to a date
function addMonthsToDate(_date,_noOfMonths) 
 {
        var yearFromDate = _date.getFullYear();
        var monthFromYear = _date.getMonth();
        var dayFromYear = _date.getDate();
        var newDate = new Date(yearFromDate, monthFromYear  + _noOfMonths, dayFromYear);
        return newDate;
 } 
//usage of the function addMonthsToDate
var todayDate = new Date();
console.log("Today's Date: " + todayDate )
console.log("New Date: " + addMonthsToDate(todayDate,13));

Output:-

Today's Date: Sat Feb 26 2022 18:20:23 GMT+0530 (India Standard Time)
New Date: Sun Mar 26 2023 00:00:00 GMT+0530 (India Standard Time)

Add Months to a Date Value using setMonth()

Javascript’s setMonth() will set the month as per the specified date in accordance to the year which is set currently.

Example1:-

Add two months to today’s date

Code:-

//function to add months to a date
function addMonthsToDate(_date,_noOfMonths) 
 {
 return new Date(_date.setMonth(_date.getMonth() + _noOfMonths));      
 }
//usage of the function addMonthsToDate
var todayDate = new Date();
console.log("Today's Date: " + todayDate )
console.log("New Date: " + addMonthsToDate(todayDate,2));

Output:-

Today's Date: Sat Feb 26 2022 18:07:53 GMT+0530 (India Standard Time)
New Date: Tue Apr 26 2022 18:07:53 GMT+0530 (India Standard Time)

Explanation:-

  • Here, we first get today’s date from the system using the new Date().
  • Access the current month according to the year of the date, using the getMonth() method.
  • The new value of the month is set in the setMonth() method. The arguments passed in this method are the number of months received in the previous step added to the number of months we want to increase (2 in our case). 
  • Finally, the Date() constructor creates a new date with the values of the day, month and year decided by the number of months being added.

Example2:-

Add 13 months to today’s date

Code:-

//function to add months to a date
function addMonthsToDate(_date,_noOfMonths) 
 {
 return new Date(_date.setMonth(_date.getMonth() + _noOfMonths));      
 }
//usage of the function addMonthsToDate
var todayDate = new Date();
console.log("Today's Date: " + todayDate )
console.log("New Date: " + addMonthsToDate(todayDate,13));

Output:-

Today's Date: Sat Feb 26 2022 18:18:33 GMT+0530 (India Standard Time)
New Date: Sun Mar 26 2023 18:18:33 GMT+0530 (India Standard Time)

I hope this article helped you add the number of months to a javascript date. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top