JavaScript Date setFullYear()
Syntax & Examples
Date.setFullYear() method
The setFullYear() method sets the full year (e.g. 4 digits for 4-digit years) for a specified date according to local time.
Syntax of Date.setFullYear()
There are 3 variations for the syntax of Date.setFullYear() method. They are:
setFullYear(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.
Returns value of type Number
.
setFullYear(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.
Returns value of type Number
.
setFullYear(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.
Returns value of type Number
.
✐ Examples
1 Using setFullYear() method with one argument
In JavaScript, we can use the setFullYear() method to set the year of a date.
For example,
- Create a new Date object with the current date and time.
- Use the setFullYear() method to set the year to 2025.
- Log the updated date to the console using console.log().
JavaScript Program
const date = new Date();
date.setFullYear(2025);
console.log(date);
Output
Expected output: The date with the year set to 2025.
2 Using setFullYear() method with two arguments
In JavaScript, we can use the setFullYear() method to set the year and month of a date.
For example,
- Create a new Date object with the current date and time.
- Use the setFullYear() method to set the year to 2025 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.setFullYear(2025, 5);
console.log(date);
Output
Expected output: The date with the year set to 2025 and the month set to June.
3 Using setFullYear() method with three arguments
In JavaScript, we can use the setFullYear() method to set the year, month, and day of a date.
For example,
- Create a new Date object with the current date and time.
- Use the setFullYear() method to set the year to 2025, 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.setFullYear(2025, 5, 15);
console.log(date);
Output
Expected output: The date with the year set to 2025, the month set to June, and the day set to 15.
Summary
In this JavaScript tutorial, we learned about setFullYear() method of Date: the syntax and few working examples with output and detailed explanation for each example.