JavaScript setFullYear

The JavaScript setFullYear function is useful for changing or setting the Year, Month, and Day of a given date as per the local system time zone.

The syntax of the setFullYear method is:

Date.setFullYear(Year, Month, Day)

Parameters: The JavaScript setFullYear method accepts three parameters.

  • Year: It accepts an integer number representing a year to update or set the year value of a given date.
  • Month (Optional): It accepts a number between 0 (January) and 11 (December) to update the month.
  • Day (Optional): It accepts a number between 0 and 31 to update the day of the month.

Return value: The setFullYear method updates the year number of the existing date to a given year. If we pass NaN or an invalid date, it returns NaN.

JavaScript setFullYear example

In the following example, we use the setFullYear method to update the current year to 2024.

const dt = new Date();
console.log(dt);
dt.setFullYear(2024);
console.log(dt);
Mon Jun 15 2026 14:28:26 GMT+0530 (India Standard Time) 
Sat Jun 15 2024 14:28:26 GMT+0530 (India Standard Time)

Example 2: In this JavaScript example, we use the date() constructor to parse a string date. Next, we use the setFullYear method to set the custom date year to 2025.

const dt = new Date("July 25, 2019 11:12:33");
console.log(dt);
dt.setFullYear(2025);
console.log(dt);
Thu Jul 25 2019 11:12:33 GMT+0530 (India Standard Time) 
Fri Jul 25 2025 11:12:33 GMT+0530 (India Standard Time)

Example 3: If we use the JavaScript setFullYear() method on a custom date without year information, it simply updates the default year 2001 to a given number. The Date() object assigns the default year 2001. Next, the setFullYear() updates 2001 to 2025.

const dt = new Date("July 25");
console.log(dt);
dt.setFullYear(2025);
console.log(dt);
Wed Jul 25 2001 00:00:00 GMT+0530 (India Standard Time) 
Fri Jul 25 2025 00:00:00 GMT+0530 (India Standard Time)

Example 4: As we mentioned earlier, both the Month Number and Day Number arguments are optional in the setFullYear Date Function.

In this example, we use the setFullYear method with three arguments to set the Full Year of a current date to 2024, the month to December, and the day of the month to 31.

const dt = new Date();
console.log(dt);
dt.setFullYear(2024, 11, 31);

console.log(dt);
console.log(dt.getFullYear());
console.log(dt.getMonth());
console.log(dt.getDate());
Mon Jun 15 2026 14:38:03 GMT+0530 (India Standard Time) 
Tue Dec 31 2024 14:38:03 GMT+0530 (India Standard Time) 
2024 
11 
31

TIP: Please use the getFullYear(), getMonth(), and getDate() methods to extract the year, month, and day of the month information from a date object.

Example 5: If we use the JavaScript setFullYear() method with months and day numbers above 11 and 31, it updates the date. In the example below, we set the year to 2025 and the month number is 20 (above 11).

  • After setting the year as 2025, the date becomes.
  • June 15, 2025 + 20 month (12 month as year + 8)
  • June 15, 2026 + month number as 8, which is September (0-based index).
const dt = new Date();
console.log(dt);
dt.setFullYear(2025, 20);
console.log(dt);
Mon Jun 15 2026 14:41:43 GMT+0530 (India Standard Time) 
Tue Sep 15 2026 14:41:43 GMT+0530 (India Standard Time)

Similarly, if we use a day of the month number above 31, it also does the same thing. The setFullYear(2025, 1) sets the date as 15th February 2025 and adds 50 days.

const dt = new Date();
console.log(dt);
dt.setFullYear(2025, 1, 50);
console.log(dt);
Mon Jun 15 2026 14:45:52 GMT+0530 (India Standard Time) 
Sat Mar 22 2025 14:45:52 GMT+0530 (India Standard Time)

Example 6: If we use the setFullYear() with an invalid date, it returns NaN.

const dt = new Date();
dt.setFullYear("New");
console.log(dt);
console.log(dt.getFullYear());
Invalid Date 
NaN