The setUTCFullYear method in JavaScript is designed to assign a year to a specific date according to Coordinated Universal Time (UTC).
Syntax
The setUTCFullYear function is defined by the syntax outlined below:
dateObj.setUTCFullYear(yearValue[, monthValue[, dateValue]])
Parameter
yearValue - This denotes an integer that indicates the specific year.
monthValue - This parameter is optional. It denotes an integer ranging from 0 to 11 that indicates the month.
dateValue - This parameter is optional. It denotes an integer ranging from 1 to 31 that indicates the specific day of the month.
JavaScript Date setUTCFullYear method example
In this section, we will explore the setUTCFullYear method by examining a variety of examples.
Example 1
Consider the following example that demonstrates how to display both the current year and the updated year.
<script>
var year=new Date();
document.writeln("Current year : "+year.getUTCFullYear()+"<br>");
year.setUTCFullYear(2019);
document.writeln("Updated year : "+year.getUTCFullYear());
</script>
Output:
Current year : 2018
Updated year : 2019
Example 2
Let’s examine an example that demonstrates how to modify the year of a specified date.
<script>
var date = new Date("August 15, 1947 20:22:10");
date.setUTCFullYear(2018);
document.writeln("Updated year : "+date.getUTCFullYear());
</script>
Output:
Updated year : 2018
Example 3
In this instance, we will additionally indicate the specific month along with the corresponding year.
<script>
var date = new Date("August 15, 1947 20:22:10");
date.setUTCFullYear(2018,12);
document.writeln("Updated year : "+date.getUTCFullYear());
</script>
Output:
Updated year : 2019