JavaScript Date setUTCFullYear() method - JavaScript Tutorial

JavaScript Date setUTCFullYear() method

BLUF: This tutorial on JavaScript Date setUTCFullYear() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Date setUTCFullYear() method

Understanding JavaScript Date setUTCFullYear() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

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:

Example

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.

Example

<script>

var year=new Date();	

document.writeln("Current year : "+year.getUTCFullYear()+"<br>");

year.setUTCFullYear(2019);	

document.writeln("Updated year : "+year.getUTCFullYear());

</script>

Output:

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.

Example

<script>

var date = new Date("August 15, 1947 20:22:10");

date.setUTCFullYear(2018);

document.writeln("Updated year : "+date.getUTCFullYear());

</script>

Output:

Output

Updated year : 2018

Example 3

In this instance, we will additionally indicate the specific month along with the corresponding year.

Example

<script>

var date = new Date("August 15, 1947 20:22:10");

date.setUTCFullYear(2018,12);

document.writeln("Updated year : "+date.getUTCFullYear());

</script>

Output:

Output

Updated year : 2019

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience