
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the Day of the Week in JavaScript
The most common techniques for getting a day of the week and the name of the day in JavaScript will be covered in this tutorial.
It is important to remember that if you are working on a small project, starting to install packages for something basic may be difficult. Small apps should be developed using JavaScript's built-in techniques; dependencies should only be included when absolutely necessary.
Following are the approaches to get the day of the week in JavaScript ?
Using the getDay() Method
Using the toLocaleString() Method
Using the getDay() Method
Use the JavaScript getDay() method to determine the day of the week.
The getDay() method of a JavaScript date returns the weekday for the given date according to local time.
An integer that is representing the day of the week is the result given by the getDay() function: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
Syntax
Following is the syntax to get a day of the week ?
let day = new Date().getDay(); //current day let day = new Date("August 6, 2022 09:38:00").getDay(); // custom day
In the above syntax, we have used the getDay() method of the Date() object.
Example
In this example, we can see how using the getDay() method, and we can print the current day of the week as per device timing. Here, we have created two-day objects.
For the day1, we are taking the computer's real-time value, and for day2, we find the day provided by the user in the program. This way, we get the day of the week using built-in methods.
Here is an example which formats a date as MM-DD-YYYY hh:mm:ss using the getDay* method. Here, Sunday is 0, Monday is 1, and so on.
<html> <body> <h3>Get the day of the week using <i>getDate() </i> method</h3> <p id = "output1"></p> <p id = "output2"></p> </body> <script> // Get current day let day1 = new Date().getDay(); document.getElementById("output1").innerHTML = day1; //current day of the week // Get day in the week of a certain date let day2 = new Date("August 6, 2022 09:38:00").getDay(); document.getElementById("output2").innerHTML = day2; //6 </script> </html>
Using the toLocaleString() Method
The toLocaleString() method returns a string representing this date in accordance with the language being used. This function merely calls Intl.DateTimeFormat in implementations that provide that API.
This function should return the language and the day's name. It can change depending on the user's browser choices by specifying the default. To retrieve the complete name of the day, we set the weekday setting in the options object to long. Short and narrow values are some other options. Pass the locale to the method as the first parameter if you wish to obtain the day's name in a different locale.
Syntax
Following is the syntax for the toLocaleString() method ?
let date = new Date(); let day = date.toLocaleString();
Above syntax shows the use of toLocaleString() method to get the day of the particular date object.
Example
In this example, we can see how using the toLocaleString() method we can print the current day of the week as per device timing. We use the weekday setting to long to get the full name of the day. In variable day1, we get the value of the current day as per computer settings. In variable day2, we get the value of the day as provided by the user in the program. This is a built-in method of JavaScript.
<html> <body> <h3>Get the day of the week using <i>getDate()</i> method</h3> <p id = "output1"></p> <p id = "output2"></p> </body> <script> // Get current day let day1 = new Date().toLocaleString('en-US', { weekday: 'long',}); document.getElementById("output1").innerHTML = day1; //current day of the week // Get day in the week of a certain date let day2 = new Date("August 6, 2022 09:38:00").toLocaleString('en-US', { weekday: 'long',}); document.getElementById("output2").innerHTML = day2; </script> </html>
In this tutorial, we have learned about how to find the day of the week using JavaScript's in-built methods getDay() and toLocaleString().
The getDay() method gives the week-day to the user's screen in the Greenwich Mean Time (GMT) zone.
The toLocaleString() shows the date according to the specific locale. The locale can be changed in the code. Both these methods show the same time for any region and device timing in the whole world.