This project calculates a date a selected number of calendar years from today. For year-based terms, use setFullYear() rather than adding 365 days, because leap years make calendar years different lengths.
const maturity = new Date();
maturity.setFullYear(maturity.getFullYear() + 3);
The original version of this project used currentDay + years * 365. That counts fixed 24-hour days, not calendar years. A term that spans February 29 can therefore land on a different calendar date than expected.
const start = new Date(2024, 0, 1);
const byDays = new Date(start);
byDays.setDate(byDays.getDate() + 365);
const byYear = new Date(start);
byYear.setFullYear(byYear.getFullYear() + 1);Read the selected number of years, clone the current Date, then change the year on the clone. Cloning keeps the original value available for display.
function calculateMaturity(years) {
const today = new Date();
const maturity = new Date(today);
maturity.setFullYear(maturity.getFullYear() + years);
return maturity;
}JavaScript normalizes invalid calendar combinations. For example, adding one year to February 29 in a leap year can move into March in a non-leap year. If a business rule instead requires the last day of February, implement that rule explicitly rather than relying on automatic normalization.
const leapDay = new Date(2024, 1, 29);
leapDay.setFullYear(2025);
console.log(leapDay.toDateString());Date Reference All Date Projects
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.