JavaScript setUTCFullYear Method





The setUTCFullYear method sets the year value of the point in time stored in a Date object according to Coordinated Universal Time.

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 setUTCFullYear method has three overloads:

myDate.setFullYear( year )

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 according to Coordinated Universal Time.
  • months being 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 according to Coordinated Universal Time. 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 according to Coordinated Universal Time. 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.


Example

The following code uses the setUTCFullYear method to change the year value of the point in time stored in Date object according to Coordinated Universal Time:

<script type="text/javascript">

var currentDate = new Date();

var outcome = "Current date, according to Coordinated Universal Time, is: <br /><br />";
outcome += currentDate.toUTCString() + "<br /><br />";

currentDate.setUTCFullYear(1776);

outcome += "After changing its year value to 1776 according to Coordinated Universal Time, the resulting date is:<br/><br/>";

outcome += currentDate.toUTCString();

document.write(outcome);

</script>


Technical Details

The setUTCFullYear 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 >> setUTCFullYear Method





© Copyright 2010-2016 ROM Cartridge