JavaScript Date getDay()
Syntax & Examples


Date.getDay() method

The getDay() method of the Date object in JavaScript returns the day of the week (0 – 6) for the specified date according to local time.


Syntax of Date.getDay()

The syntax of Date.getDay() method is:

getDay()

This getDay() method of Date returns the day of the week (0 – 6) for the specified date according to local time.

Return Type

Date.getDay() returns value of type Number.



✐ Examples

1 Using getDay() to get the day of the week

In JavaScript, we can use the getDay() method to get the day of the week from a Date object.

For example,

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

JavaScript Program

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

Output

5

2 Using getDay() with the current date

In JavaScript, we can use the getDay() method to get the current day of the week from the current date.

For example,

  1. We create a new Date object today representing the current date.
  2. We use the getDay() method to get the current day of the week from today.
  3. The current day of the week is stored in the variable currentDayOfWeek.
  4. We log currentDayOfWeek to the console using console.log() method.

JavaScript Program

const today = new Date();
const currentDayOfWeek = today.getDay();
console.log(currentDayOfWeek);

Output

0

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

In JavaScript, we can use the getDay() method to get the day of the week from a specific date in the past.

For example,

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

JavaScript Program

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

Output

4

Summary

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