JavaScript Date getUTCMonth() Method



The JavaScript Date getUTCMonth() method does not accept any parameters and will retrieve the minute component from the date object according to the universal time. The return value will be an integer between 0 to 11 (where 0 indicates the first month of the year and 11 indicates the last month). If the provided Date object is invalid, this method will return Not a Number (NaN) as result.

Syntax

Following is the syntax of JavaScript Date getUTCMonth() method −

getUTCMonth();

This method does not accept any parameters.

Return Value

This method returns an integer between 0 and 11 representing the month in UTC time.

Example 1

The following example shows how the JavaScript Date getUTCMonth() method will work −

<html>
<body>
<script>
   const currentDate = new Date();
   const currentMonth = currentDate.getMonth();

   document.write(currentMonth);
</script>
</body>
</html>

Output

It returns the month component of a date according to universal time.

Example 2

In this example, we are using getUTCMonth() method to retrieve the month value from a specific date ('2023-10-21') −

<html>
<body>
<script>
   const specificDate = new Date('2023-10-21');
   const monthOfDate = specificDate.getMonth();

   document.write(monthOfDate);
</script>
</body>
</html>

Output

This will return "09" as the month value for the provided date.

Example 3

In here, we are retrieving the current month name through a function −

<html>
<body>
<script>
   function getMonthName(date) {
      const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
      return months[date.getUTCMonth()];
   }

   const currentDate = new Date();
   const currentMonthName = getMonthName(currentDate);
   document.write(currentMonthName);
</script>
</body>
</html>

Output

It returns the name of the month according to the universal time.

Advertisements