Weekday as a number in JavaScript?

JavascriptObject Oriented ProgrammingFront End Scripts

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.

Live Demo

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

Output

1


Example-2

Live Demo

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

Output

1
raja
Published on 05-Aug-2019 14:11:45
Advertisements