2

JavaScript: Add Month to a Date

 2 years ago
source link: https://thispointer.com/javascript-add-month-to-a-date/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:-

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0NwM1OTE5MCZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDMmMxQmMDM0NUYmMDMmN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2NC42Ml4kNwQzqXNypyVBPU1irzyfoGEyMxY1LwAyMwAyMwuYMTEyM0IyMwBMnW51rCUlMHt4Ny82NCUlOSUlMEFjpGkyV2VvS2y0JTJGNTM3LwM2JTIjJTI4S0uUTUjyMxMyMwBfnWgyJTIjR2Vwn28yMwxyMwBDnHJioWUyMxY3Nl4jLwM4NwUhMTIjJTIjU2FzYXJcJTJGNTM3LwM2JzNmqXVcZD02MwIkNmI5NTIkNGZuJzNioaRyoaRGnWkySWQ9MCZgZWRcYVBfYXyMnXN0SWQ9MCZgZWRcYUkcp3RJZD0jJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2NDYmNTxkOTE5NDpzqWyxPVNyn2yhZG9TUGkurWVlNwIlMTplOTY0OTI4OCZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZdYXZup2NlnXB0LWFxZC1go250nC10ol1uLWRuqGUyMxYzZzkiYXRTqGF0qXM9ZzFfp2UzZWyxp3A9pHJyYzyx

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));
// 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)
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 thenew 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));
//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)
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));
//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)
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));
//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)
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 !!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK