JavaScript Date getUTCMonth()
Syntax & Examples


Date.getUTCMonth() method

The getUTCMonth() method of the Date object in JavaScript returns the month (0 – 11) in the specified date according to universal time.


Syntax of Date.getUTCMonth()

The syntax of Date.getUTCMonth() method is:

getUTCMonth()

This getUTCMonth() method of Date returns the month (0 – 11) in the specified date according to universal time.

Return Type

Date.getUTCMonth() returns value of type Number.



✐ Examples

1 Using getUTCMonth() to get the UTC month of a specific date

In JavaScript, we can use the getUTCMonth() method to get the month from a Date object according to universal time.

For example,

  1. We create a new Date object date representing January 15, 2021.
  2. We use the getUTCMonth() method to get the month from date.
  3. The month is stored in the variable utcMonth.
  4. We log utcMonth to the console using console.log() method.

JavaScript Program

const date = new Date('2021-01-15');
const utcMonth = date.getUTCMonth();
console.log(utcMonth);

Output

0

2 Using getUTCMonth() with the current date

In JavaScript, we can use the getUTCMonth() method to get the current month from the current date according to universal time.

For example,

  1. We create a new Date object now representing the current date.
  2. We use the getUTCMonth() method to get the current month from now.
  3. The current month is stored in the variable currentUTCMonth.
  4. We log currentUTCMonth to the console using console.log() method.

JavaScript Program

const now = new Date();
const currentUTCMonth = now.getUTCMonth();
console.log(currentUTCMonth);

Output

5

3 Using getUTCMonth() with a specific date in the past

In JavaScript, we can use the getUTCMonth() method to get the month from a specific date in the past according to universal time.

For example,

  1. We create a new Date object pastDate representing July 4, 1776.
  2. We use the getUTCMonth() method to get the month from pastDate.
  3. The month is stored in the variable pastUTCMonth.
  4. We log pastUTCMonth to the console using console.log() method.

JavaScript Program

const pastDate = new Date('1776-07-04');
const pastUTCMonth = pastDate.getUTCMonth();
console.log(pastUTCMonth);

Output

6

Summary

In this JavaScript tutorial, we learned about getUTCMonth() method of Date: the syntax and few working examples with output and detailed explanation for each example.