• JavaScript Video Tutorials

JavaScript Date setUTCFullYear() Method



The JavaScript Date.setUTCFullYear() method is used to set the year of a Date object according to Coordinated Universal Time (UTC). This method allows you to modify the year component of a Date object without changing other date components such as month or day.

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.

Syntax

Following is the syntax of JavaScript Date setUTCFullYear() method −

setUTCFullYear(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 return value of setUTCFullYear() is the number of milliseconds between January 1, 1970, and the updated Date object after setting the year.

Example 1

In the following example, we are using the JavaScript Date setUTCFullYear() method to set the year of the Date object to 2023 −

<html>
<body>
<script>
   const currentDate = new Date();
   currentDate.setUTCFullYear(2025);

   document.write(currentDate);
</script>
</body>
</html>

Output

If we execute the above program, the year will be set to 2023.

Example 2

Here, we are setting the year to (2023), month to 10 (November), and day to (25) −

<html>
<body>
   const specificDate = new Date();
   specificDate.setUTCFullYear(2024, 10, 25); //Will set to November 25, 2024

   document.write(specificDate);
</script>
</body>
</html>

Output

After executing, this program returns a timestamp with the provided date.

Example 3

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('2023-11-15'); //December 15 2023
   existingDate.setUTCFullYear(2023, 12, 15); // It will be January 15 2024

   document.write(existingDate);
</script>
</body>
</html>

Output

It returns a timestamp as "Sun Jan 15 2024 05:30:00 GMT+0530 (India Standard Time)".

Example 4

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('2024-03-30'); //March 2024 has 31 days.
   existingDate.setUTCFullYear(2024, 2, 32); 

   document.write(existingDate);
</script>
</body>
</html>

Output

It returns a timestamp as "Mon Apr 01 2024 05:30:00 GMT+0530 (India Standard Time)".

Example 5

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.setUTCFullYear("dfbgf");

   document.write(currentDate.getUTCFullYear());
</script>
</body>
</html>

Output

As we can see the output, "NaN" is returned as output.

Advertisements