Javascript Reference - JavaScript getDay() Method








getDay()
Returns the date's day of the week as a number (where 0 represents Sunday and 6 represents Saturday).

Sunday is 0, Monday is 1, and so on.

Browser Support

getDay() Yes Yes Yes Yes Yes

Syntax

dateObject.getDay();

Parameters

None.





Return Value

A number from 0 to 6 to represent the day of the week.

Sunday is 0, Monday is 1, and so on.

Example


var myDate = new Date();

console.log(myDate.getDay());

The code above generates the following result.

Example 2

The following code returns the name of the weekday defined in an array.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from   ww  w.  j  ava  2 s  .  c  o m-->
    var d = new Date();
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var n = weekday[d.getDay()];
    document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>

The code above is rendered as follows: