published: 20 Sep 2022
2 min read
How to add years to a date in JavaScript
To add one or more years to date in JavaScript:
- Use the
getFullYear()
method to get the year of the given date. - Use the
setFullYear()
method by passing the result returned bygetFullYear()
plus the number of years you want to add. - 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)
console.log(date)
// 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)
console.log(date.toUTCString())
// 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)
console.log(result.toUTCString())
// 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.
Are we missing something? Help us improve this article. Reach out to us.
How to add years to a date in JavaScript
To add one or more years to date in JavaScript:
- Use the
getFullYear()
method to get the year of the given date. - Use the
setFullYear()
method by passing the result returned bygetFullYear()
plus the number of years you want to add. - 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)
console.log(date)
// 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)
console.log(date.toUTCString())
// 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)
console.log(result.toUTCString())
// 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.
Are you looking for other code tips?
JS Nooby
Javascript connoisseur