How to use Date.getDay() method In JQuery

In this approach, we will use the Date.getDay() method of the JavaScript Date() constructor to get the current day of the week by using it with an array that contains the names of the weekdays.

Example: The below code will explain the use of the Date.getDay() method to extract the day of the week

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        Get Day of the Week
    </title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
    </script>
</head>
 
<body>
 
    <script>
        $(document).ready(function () {
            const days =
                ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
                'Thursday', 'Friday', 'Saturday'];
            const date = new Date();
            const dayOfWeek = days[date.getDay()];
            console.log("Today is " + dayOfWeek);
        });
    </script>
 
</body>
 
</html>


Output:

How to get the Day of the Week in jQuery ?

The Date() constructor will not give the day of the week directly. It returns a number for the weekdays starting from 0 to 6. You need to implement some extra methods in jQuery to get the day of the week as listed below:

Table of Content

  • Using Date.getDay() method
  • Using moment.js

Similar Reads

Using Date.getDay() method

In this approach, we will use the Date.getDay() method of the JavaScript Date() constructor to get the current day of the week by using it with an array that contains the names of the weekdays....

Using moment.js

...

Contact Us