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,
- We create a new Date object
date
representing January 15, 2021. - We use the
getUTCMonth()
method to get the month fromdate
. - The month is stored in the variable
utcMonth
. - We log
utcMonth
to the console usingconsole.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,
- We create a new Date object
now
representing the current date. - We use the
getUTCMonth()
method to get the current month fromnow
. - The current month is stored in the variable
currentUTCMonth
. - We log
currentUTCMonth
to the console usingconsole.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,
- We create a new Date object
pastDate
representing July 4, 1776. - We use the
getUTCMonth()
method to get the month frompastDate
. - The month is stored in the variable
pastUTCMonth
. - We log
pastUTCMonth
to the console usingconsole.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.