JavaScript Date setUTCFullYear()
Syntax & Examples
Date.setUTCFullYear() method
The setUTCFullYear() method sets the full year (e.g., 4 digits for 4-digit years) for a specified date according to universal time.
Syntax of Date.setUTCFullYear()
There are 3 variations for the syntax of Date.setUTCFullYear() method. They are:
setUTCFullYear(yearValue)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
yearValue | required | An integer specifying the numeric value of the year, for example, 1995. |
This method sets the full year for the specified date according to universal time.
Returns value of type Number
.
setUTCFullYear(yearValue, monthValue)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
yearValue | required | An integer specifying the numeric value of the year, for example, 1995. |
monthValue | optional | An integer between 0 and 11 representing the months January through December. |
This method sets the full year and month for the specified date according to universal time.
Returns value of type Number
.
setUTCFullYear(yearValue, monthValue, dateValue)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
yearValue | required | An integer specifying the numeric value of the year, for example, 1995. |
monthValue | optional | An integer between 0 and 11 representing the months January through December. |
dateValue | optional | An integer between 1 and 31 representing the day of the month. |
This method sets the full year, month, and day for the specified date according to universal time.
Returns value of type Number
.
✐ Examples
1 Using setUTCFullYear() method with one argument
In JavaScript, we can use the setUTCFullYear() method to set the year of a date according to universal time.
For example,
- Create a new Date object with the current date and time.
- Use the setUTCFullYear() method to set the year to 2025 UTC.
- Log the updated date to the console using console.log().
JavaScript Program
const date = new Date();
date.setUTCFullYear(2025);
console.log(date);
Output
Expected output: The date with the year set to 2025 UTC.
2 Using setUTCFullYear() method with two arguments
In JavaScript, we can use the setUTCFullYear() method to set the year and month of a date according to universal time.
For example,
- Create a new Date object with the current date and time.
- Use the setUTCFullYear() method to set the year to 2025 UTC and the month to June (month value 5).
- Log the updated date to the console using console.log().
JavaScript Program
const date = new Date();
date.setUTCFullYear(2025, 5);
console.log(date);
Output
Expected output: The date with the year set to 2025 UTC and the month set to June.
3 Using setUTCFullYear() method with three arguments
In JavaScript, we can use the setUTCFullYear() method to set the year, month, and day of a date according to universal time.
For example,
- Create a new Date object with the current date and time.
- Use the setUTCFullYear() method to set the year to 2025 UTC, the month to June (month value 5), and the day to 15.
- Log the updated date to the console using console.log().
JavaScript Program
const date = new Date();
date.setUTCFullYear(2025, 5, 15);
console.log(date);
Output
Expected output: The date with the year set to 2025 UTC, the month set to June, and the day set to 15.
Summary
In this JavaScript tutorial, we learned about setUTCFullYear() method of Date: the syntax and few working examples with output and detailed explanation for each example.