JavaScript: Add Year to a Date

Date() constructor creates different date values. At the same time, while we are working with dates, we often need to process the dates by adding a year or some years to them. This article will discuss different ways to add years to a javascript date.

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

Add one year to today’s date

Code:-

// function to add no of years to date
function addYearsToDate(_date,_noOfYears) 
 {
        var yearFromDate = _date.getFullYear();
        var monthFromYear = _date.getMonth();
        var dayFromYear = _date.getDate();
        var newDate = new Date(yearFromDate + _noOfYears, monthFromYear, dayFromYear);
        return newDate;
 }   
//usage of the function addYearsToDate
var todayDate = new Date();
console.log("today's date: " + todayDate )
console.log("year after today: " + addYearsToDate(todayDate,1));

Output:-

today's date: Thu Feb 10 2022 18:34:52 GMT+0530 (India Standard Time)
year after today: Fri Feb 10 2023 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 gets the month from today’s date.
  • getDate() method gets the day from today’s date.
  • After accessing all the above values, create a new date by Date() constructor, which has the same value of day and month, but the year value is added by 1.

Add Year to a Date Value using setFullYear()

Javascript’s setFullYear() will set the year value for a specified date according to the local time. This method returns a new timestamp.

Example1:-

Add one year to today’s date

Code:-

//function to add year to a date
function addYearsToDate(_date,_noOfYears) 
 {
 return new Date(_date.setFullYear(_date.getFullYear() + _noOfYears));      
 }
//usage of the function addYearsToDate
var todayDate = new Date();
console.log("today's date: " + todayDate )
console.log("year after today: " + addYearsToDate(todayDate,1));

Output:-

today's date: Thu Feb 10 2022 18:47:49 GMT+0530 (India Standard Time)
year after today: Fri Feb 10 2023 18:47:49 GMT+0530 (India Standard Time)

Explanation:-

  • Here, we are first getting today’s date from the system using new Date().
  • Then on this date set the year value by accessing the current year using the getFullYear() method and adding the number of years to it (1 in our case). 
  • The new value of the year is set using the setYearValue() method.

Example2:-

Add 5 years to today’s date

Code:-

//function to add year to a date
function addYearsToDate(_date,_noOfYears) 
 {
 return new Date(_date.setFullYear(_date.getFullYear() + _noOfYears));      
 }
//usage of the function addYearsToDate
var todayDate = new Date();
console.log("today's date: " + todayDate )
console.log("year after today: " + addYearsToDate(todayDate,5));

Output:-

today's date: Thu Feb 10 2022 19:23:40 GMT+0530 (India Standard Time)
5 years from today: Wed Feb 10 2027 19:23:40 GMT+0530 (India Standard Time)

Example3:-

Add 1 year to a specified date : 14th November 2014

Code:-

//function to add year to a date
function addYearsToDate(_date,_noOfYears) 
 {
 return new Date(_date.setFullYear(_date.getFullYear() + _noOfYears));      
 }
//usage of the function addYearsToDate
var date = new Date("2014-11-14");
console.log("New Date: " + addYearsToDate(date,1));

Output:-

New Date: Sat Nov 14 2015 05:30:00 GMT+0530 (India Standard Time)

I hope this article helped you add a number of years 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