Method
JavaScript Date setUTCFullYear() Method
The setUTCFullYear()
method of the JavaScript Date object is used to set the year of the specified date according to universal time. This method is used to set the year value in the Date object and returns the number of milliseconds since January 1, 1970 00:00:00 UTC.
Syntax
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
dateObj.setUTCFullYear(year[, month[, date]])
[/dm_code_snippet]
Parameters
This method takes three parameters:
year
− An integer representing the year in universal time.month
− An integer representing the month in universal time. The value is between 0 (January) and 11 (December).date
− An optional integer representing the day of the month in universal time. The value is between 1 and 31.
Return Value
This method returns the number of milliseconds since January 1, 1970 00:00:00 UTC.
Example
Let’s see an example to understand how setUTCFullYear()
method works:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
<html>
<body>
<p>Click the button to display the UTC full year of the date.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
d.setUTCFullYear(2020);
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
[/dm_code_snippet]
Output
When the above code is executed, it will produce the following result:
Click the button to display the UTC full year of the date.
Try it
Mon Jan 13 2020 00:00:00 GMT+0530 (India Standard Time)