How to add years to a date in JavaScript

To add one or more years to date in JavaScript:

  1. Use the getFullYear() method to get the year of the given date.
  2. Use the setFullYear() method by passing the result returned by getFullYear() plus the number of years you want to add.
  3. The setFullYear() method sets the value on the given date.

Here is an example that adds a year to the current date in JavaScript:

const date = new Date()
date.setFullYear(date.getFullYear() + 1)

// Sun Sep 10 2023 23:19:38 GMT+0500 (Pakistan Standard Time)

The getFullYear() method returns the year of the given date in 4 digits format, for example,2022.

The setFullYear() method takes an integer as input, representing the year, and sets the value on the date.

The above methods also take care of the situation where adding a specific number of years to date results in the next day, month, or year:

const date = new Date('2022-09-10T23:33:45.900Z')
date.setFullYear(date.getFullYear() + 3)

// Wed, 10 Sep 2025 23:33:45 GMT

If you frequently need to add years to date, create a reusable function that takes the number of years as a parameter and adds them to the current date:

Date.prototype.addYears = function (years) {
  const date = new Date(this.valueOf())
  date.setFullYear(date.getFullYear() + years)
  return date

const date = new Date('2022-09-10T16:23:45.900Z')

// Add 5 Years
const result = date.addYears(5)

// Fri, 10 Sep 2027 16:23:45 GMT

In the above example, we added a function called addYears() to the Date object prototype. This function will be available to all instances of Date for adding years to date.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.