JavaScript Date getDay()

Posted by

JavaScript Date getDay() Method

The getDay() method in JavaScript is used to get the day of the week for a given date. This method returns an integer number that corresponds to the day of the week (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.).

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

dateObj.getDay()

[/dm_code_snippet]

Where dateObj is a valid Date object.

Examples

Let’s take a look at some examples of using the getDay() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Get the day of the week for today
var today = new Date();
var dayOfWeek = today.getDay();

// Output the day of the week
console.log("Today is " + dayOfWeek)

[/dm_code_snippet]

The above code will output Today is 0 (0 = Sunday).

You can also use the getDay() method with a specified date:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Get the day of the week for June 15, 2020
var date = new Date("June 15, 2020");
var dayOfWeek = date.getDay();

// Output the day of the week
console.log("June 15, 2020 is " + dayOfWeek)

[/dm_code_snippet]

The above code will output June 15, 2020 is 1 (1 = Monday).

Browser Support

The getDay() method is supported in the following browsers:

  • Chrome
  • Edge
  • Firefox
  • Internet Explorer
  • Opera
  • Safari

Further Reading

For more information about the getDay() method, you can read the MDN documentation.