Sometimes, we want to add a year to today’s date with JavaScript.
In this article, we’ll look at how to add a year to today’s date with JavaScript.
How to add a year To today’s date with JavaScript?
To add a year to today’s date with JavaScript, we can use the date’s getFullYear
and setFullYear
methods.
For instance, we write
const aYearFromNow = new Date();
aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 1);
console.log(aYearFromNow);
to create the aYearFromNow
Date
object with the current date and time.
Then we call aYearFromNow.getFullYear
to get the 4 digit year number of the date.
Then we add 1 to it and use the sum as the argument for setFullYear
to increment the year of aYearFromNow
by 1.
Conclusion
To add a year to today’s date with JavaScript, we can use the date’s getFullYear
and setFullYear
methods.