setFullYear()
ES1+Sets the full year for a specified date according to local time.
Syntax
date.setFullYear(year, month, day)Parameters
year number An integer specifying the year
month number optionalAn integer between 0 and 11 representing the month
day number optionalAn integer between 1 and 31 representing the day of the month
Return Value
The number of milliseconds between the date and UNIX epoch
Examples
const date = new Date('2024-01-15');
date.setFullYear(2025);
console.log(date.getFullYear());
date.setFullYear(2026, 5, 20);
console.log(date.toLocaleDateString('ko-KR')); 📌 When to Use
Use setFullYear() when you need to modify the year of an existing Date object, such as for date navigation or year-based calculations.
⚠️ Common Mistakes
Forgetting that setFullYear() mutates the original Date object instead of returning a new one.
Not accounting for leap year when setting February 29th on non-leap years.
✅ Best Practices
Clone the date before modifying if you need to preserve the original: new Date(date.getTime()).
Use setFullYear(year, month, day) to set all date components at once for atomicity.
⚡ Performance Notes
setFullYear() is O(1). It is more efficient than creating a new Date object for simple year changes.
🌍 Real World Example
Year Navigation Component
Navigate between years in a date picker or calendar component.
function createYearNavigator(initialDate) {
let currentDate = new Date(initialDate);
return {
current: () => new Date(currentDate),
goToYear: (year) => {
const newDate = new Date(currentDate);
newDate.setFullYear(year);
currentDate = newDate;
return currentDate;
},
nextYear: () => {
const newDate = new Date(currentDate);
newDate.setFullYear(newDate.getFullYear() + 1);
currentDate = newDate;
return currentDate;
},
prevYear: () => {
const newDate = new Date(currentDate);
newDate.setFullYear(newDate.getFullYear() - 1);
currentDate = newDate;
return currentDate;
}
};
}
const navigator = createYearNavigator('2024-06-15');
console.log(navigator.nextYear().getFullYear()); // 2025
console.log(navigator.goToYear(2020).getFullYear()); // 2020