Computer >> Computer tutorials >  >> Programming >> Javascript

Weekday as a number in JavaScript?


Weekday

Every day in a week, which is from Monday to Sunday, has a number starting from 1 to 7 respectively. Javascript date object has provided the getDay() method to get the day of the week. Let's discuss it in a nutshell.

syntax

var d = getDay();

It won't take any parameters and just provides the weekday. If the day is Monday then 1 will be provided, if Tuesday then 2 will be provided and so on.

Example-1

In the following example, using the date object and its method getDay() the weekday of the day is calculated. The day of the operation of my code is Monday. So the output executed is 1.

<html>
<body>
<script>
   var d = new Date();
   document.write(d.getDay());
</script>
</body>
</html>

Output

1


Example-2

<html>
<body>
<p id = "day"></p>
<script>
   var d = new Date();
   document.getElementById("day").innerHTML = (d.getDay());
</script>
</body>
</html>

Output

1