The setFullYear method sets the year value of the point in time stored in a Date object.
Besides, it can also be used to change its month, its day of the month, or to modify the date by increasing or decreasing it in a provided quantity of months or days.
The value returned by this method is the quantity of milliseconds passed since January 1st 1970 until the point in time stored in the Date object once it has been modified.
Syntax
The setFullYear method has three overloads:
myDate.setFullYear( year )
myDate.setFullYear( year, months )
myDate.setFullYear( year, months, days )
myDate.setFullYear( year, months )
myDate.setFullYear( year, months, days )
With:
- year being a required parameter that sets the year of the point in time stored in a Date object.
- months being an optional parameter that consists of an integer number that, when it is between 0 and 11, changes only the month value of the point in time stored in a Date object. Any month value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining months. If the parameter is an integer between 0 and 11 but the day of the month in the Date object does not exist for the month being indicated by the parameter, once the new month is assigned the point in time will be increased in a quantity equal to the days remaining in the parameter.
- days being an optional parameter that consists of an integer number that, when it is between 1 and the last day of the month, changes only the day of the month of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining days.
- myDate being an object based on the Date object.
Instead of being instantiated from a class, in JavaScript objects are created by replicating other objects. This paradigm is called Prototype-based programming.
Example
The following code uses the setFullYear method to change the year value of the point in time stored in Date object:
<script type="text/javascript">
var currentDate = new Date();
var outcome = "Current date is: <br /><br />";
outcome += currentDate + "<br /><br />";
currentDate.setFullYear(1776);
outcome += "After changing its year value to 1776, the resulting date is:<br/><br/>";
outcome += currentDate;
document.write(outcome);
</script>
var currentDate = new Date();
var outcome = "Current date is: <br /><br />";
outcome += currentDate + "<br /><br />";
currentDate.setFullYear(1776);
outcome += "After changing its year value to 1776, the resulting date is:<br/><br/>";
outcome += currentDate;
document.write(outcome);
</script>
Technical Details
The setFullYear method belongs to the Date object, it is available since the version 1.3 of JavaScript, and it is supported in all major browsers.
JavaScript Manual >> Date Object >> setFullYear Method