JavaScript Date getUTCDay()
Syntax & Examples
Date.getUTCDay() method
The getUTCDay() method of the Date object in JavaScript returns the day of the week (0 – 6) in the specified date according to universal time.
Syntax of Date.getUTCDay()
The syntax of Date.getUTCDay() method is:
getUTCDay()
This getUTCDay() method of Date returns the day of the week (0 – 6) in the specified date according to universal time.
Return Type
Date.getUTCDay() returns value of type Number
.
✐ Examples
1 Using getUTCDay() to get the UTC day of the week of a specific date
In JavaScript, we can use the getUTCDay()
method to get the day of the week from a Date object according to universal time.
For example,
- We create a new Date object
date
representing January 15, 2021. - We use the
getUTCDay()
method to get the day of the week fromdate
. - The day of the week is stored in the variable
utcDay
. - We log
utcDay
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('2021-01-15');
const utcDay = date.getUTCDay();
console.log(utcDay);
Output
5
2 Using getUTCDay() with the current date
In JavaScript, we can use the getUTCDay()
method to get the current day of the week from the current date according to universal time.
For example,
- We create a new Date object
now
representing the current date and time. - We use the
getUTCDay()
method to get the current day of the week fromnow
. - The current day of the week is stored in the variable
currentUTCDay
. - We log
currentUTCDay
to the console usingconsole.log()
method.
JavaScript Program
const now = new Date();
const currentUTCDay = now.getUTCDay();
console.log(currentUTCDay);
Output
0
3 Using getUTCDay() with a specific date in the past
In JavaScript, we can use the getUTCDay()
method to get the day of the week 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
getUTCDay()
method to get the day of the week frompastDate
. - The day of the week is stored in the variable
pastUTCDay
. - We log
pastUTCDay
to the console usingconsole.log()
method.
JavaScript Program
const pastDate = new Date('1776-07-04');
const pastUTCDay = pastDate.getUTCDay();
console.log(pastUTCDay);
Output
4
Summary
In this JavaScript tutorial, we learned about getUTCDay() method of Date: the syntax and few working examples with output and detailed explanation for each example.