JavaScript Date setFullYear() Method
The setFullYear() method is a built-in function in JavaScript that is used to set the "year" component of a Date object. The return value of this method is the updated timestamp of the Date object, which reflects the changes made by modifying the year. Optionally, we can also modify the month and day values of the date object.
If any of the parameter passed to this method is NaN or undefined, the date is set to "invalid date" and Not a Number (NaN) will be returned as result.
Syntax
Following is the syntax of JavaScript Date setFullYear() method −
setFullYear(yearValue, monthValue, dateValue);
Parameters
This method accepts three parameters. The same is described below −
- yearValue − An integer represents a new year (four digits).
- monthValue (optional) − An integer between 0 and 11, where 0 is January and 11 is December.
- If -1 is provided, it will result in the last month of the previous year.
- If 12 is provided, it will result in the first month of the next year.
- dateValue (optional) − An integer between 1 and 31.
- If 0 is provided, it will result in the last day of the previous month.
- If -1 is provided, it will result in day before the last day of the previous month.
- If 32 is provided, it will result in the first day of the next month (if that month has 31 days).
- If 32 is provided, it will result in the second day of the next month (if that month has 30 days).
Return Value
The setFullYear() method returns the timestamp of the updated date object after setting the year.
Example 1
In the following example, we are using the JavaScript Date setFullYear() method to set the year of the Date object to 2023 −
<html> <body> <script> const currentDate = new Date(); currentDate.setFullYear(2023); document.write(currentDate); </script> </body> </html>
Output
If we execute the above program, the year will be set to 2023, and the month and day will be according to the local time.
Example 2
Here, we are setting the year to (2023), month to 10 (November), and day to (25) −
<html> <body> <script> const specificDate = new Date(); specificDate.setFullYear(2023, 10, 25); document.write(specificDate); </script> </body> </html>
Output
After executing, this program returns a timestamp with the provided date.
Example 3
In this example, we are changing only the year while keeping the existing month (May) and day (15) unchanged.
<html>
<body>
<script>
const existingDate = new Date('2022-05-15');
existingDate.setFullYear(2023);
document.write(existingDate);
</script>
</body>
</html>
Output
It returns a timestamp as "Mon May 15 2023 05:30:00 GMT+0530 (India Standard Time)".
Example 4
If we specify "12" for a monthValue, the year will incremented by 1 (yearValue + 1) and 0 will be used for the month.
<html>
<body>
<script>
const existingDate = new Date('2022-11-15'); //December 15 2022
existingDate.setFullYear(2022, 12, 15); // It will be January 15 2023
document.write(existingDate);
</script>
</body>
</html>
Output
It returns a timestamp as "Sun Jan 15 2023 05:30:00 GMT+0530 (India Standard Time)".
Example 5
If we specify "32" for a dateValue, the month will incremented by 1 (if that month has 31 days) and will result in the first day of the next month.
<html>
<body>
<script>
const existingDate = new Date('2023-10-30'); //October 2023 has 31 days.
existingDate.setFullYear(2023, 9, 32);
document.write(existingDate);
</script>
</body>
</html>
Output
It returns a timestamp as "Wed Nov 01 2023 05:30:00 GMT+0530 (India Standard Time)".
Example 6
If we pass a invalid date value as a parameter to this function, the date will be set to "Invalid date" and "NaN" is returned as result −
<html>
<body>
<script>
const currentDate = new Date();
currentDate.setFullYear("dfbgf");
document.write(currentDate.getUTCFullYear());
</script>
</body>
</html>
Output
As we can see the output, "NaN" is returned as output.