JavaScript Date setUTCMonth() method - JavaScript Tutorial

JavaScript Date setUTCMonth() method

BLUF: This tutorial on JavaScript Date setUTCMonth() 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 setUTCMonth() method

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

The setUTCMonth function in JavaScript is utilized to assign an integer that denotes the month for a given date, using Coordinated Universal Time (UTC). The integer values for the setUTCMonth method commence at 0, which corresponds to January.

Syntax

The syntax for the setUTCMonth method is illustrated as follows:

Example

dateObj.setUTCMonth(monthValue[, dayValue])

Parameter

monthValue - This denotes an integer ranging from 0 to 11 that indicates the month.

dayValue - This parameter is optional. It denotes an integer ranging from 1 to 31 that indicates the specific day of the month.

JavaScript Date setUTCMonth method example

In this section, we will explore the setUTCMonth method by examining a variety of examples.

Example 1

Let us examine an example that demonstrates how to display both the current month and the updated month.

Example

<script>

var month=new Date();	

document.writeln("Current month : "+(month.getUTCMonth()+1)+"<br>");

month.setUTCMonth(8);

document.writeln("Updated month : "+(month.getUTCMonth()+1));

</script>

Output:

Output

Current month : 8

Updated month : 9

Example 2

Let's examine an example that demonstrates how to modify the month of a specified date.

Example

<script>

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

month.setUTCMonth(10);

document.writeln("Updated month : "+(month.getUTCMonth()+1));

</script>

Output:

Example 3

In this illustration, we will additionally indicate the specific day along with the month.

Example

<script>

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

month.setUTCMonth(10,31);

document.writeln("Updated month : "+(month.getUTCMonth()+1));

</script>

Output:

Output

Updated month : 12

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