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,

  1. We create a new Date object date representing January 15, 2021.
  2. We use the getUTCDay() method to get the day of the week from date.
  3. The day of the week is stored in the variable utcDay.
  4. We log utcDay to the console using console.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,

  1. We create a new Date object now representing the current date and time.
  2. We use the getUTCDay() method to get the current day of the week from now.
  3. The current day of the week is stored in the variable currentUTCDay.
  4. We log currentUTCDay to the console using console.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,

  1. We create a new Date object pastDate representing July 4, 1776.
  2. We use the getUTCDay() method to get the day of the week from pastDate.
  3. The day of the week is stored in the variable pastUTCDay.
  4. We log pastUTCDay to the console using console.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.