After learning JavaScript’s built-in methods, we found several methods to check if a date is during the weekend using JavaScript. Please read the instructions below to know the methods we apply.
Check if a date is during the weekend using Javascript
Combine toString() and substring() methods
toString() method
This method will return a date object as a string.
Syntax:
Date.toString()
Parameter:
- None
substr() method
This method takes part of a string without affecting the parent string.
Syntax:
string.substr(start, length)
Parameters:
- start: Start position to get a string part.
- end: The number of characters you want to get.
Please look at the example below to understand how we do it.
Example:
// Create a date object with a string var myDate = new Date("2022-11-27"); // Create a date object with a string var todayDate = new Date("2022-12-02"); // Check the weekend function checkIsWeekend(myDate) { var convertDate = myDate.toString(); console.log("convertDate: ", convertDate); var getDay = convertDate.substr(0, 3); if (getDay === "Sun" || getDay === "Sat") { return true; } else { return false; } } console.log("Is todayDate a weekend?", checkIsWeekend(todayDate)); console.log("Is myDate a weekend?", checkIsWeekend(myDate));
Output
convertDate: Fri Dec 02 2022 07:00:00 GMT+0700
Is todayDate a weekend? false
convertDate: Sun Nov 27 2022 07:00:00 GMT+0700
Is myDate a weekend? true
To check if a date is during the weekend using JavaScript, we first convert the Date object to a string using the toString() method, then we use the substr() method with 2 parameters, 0 and 3, to get the date of the string (the first 3 characters are used to identify the date).
We take the string obtained from the substr() method and then compare it with the string “Sat” (corresponding to Saturday is the weekend) or “Sun” (corresponding to Sunday is the weekend); If true, return true; If false, return false.
Use getDay() method
This method will return the number from 0 -> 6 corresponding to a day of the week. Sunday = 0, Monday = 1, and the numbers corresponding to the following days.
Syntax:
Date.getDay()
Parameter:
- None
Example:
// Create a date object with a string var myDate = new Date("2022-11-27"); // Create a date object with a string var todayDate = new Date("2022-12-02"); // Check the weekend function checkIsWeekend(myDate) { switch (myDate.getDay()) { // 0 is Sunday case 0: return true; // 0 is Saturday case 6: return true; default: return false; } } console.log("Is todayDate a weekend?", checkIsWeekend(todayDate)); console.log("Is myDate a weekend?", checkIsWeekend(myDate));
Output
Is todayDate a weekend? false
Is myDate a weekend? true
In this example, we could use an if else statement but we use a switch statement because we want to innovate a bit. We use the getDay() method to get the number corresponding to the day of the week. And use the switch case statement to see how the case. If it is 0 (corresponding to Sunday) or 6 (corresponding to Saturday), it returns true; otherwise, it returns false.
Summary
This article of ours used the toString method in combination with the substr() method, the date object’s getDay method, to check if a date is during the weekend using Javascript. If you want to see more tutorials like this, follow us, we have lots of detailed and easy-to-understand tutorials for you.
Maybe you are interested:
- Check if a Date is less than 1 Hour ago using JavaScript
- Check if a Date is after another Date using JavaScript
- Check if a Date is before Today’s date in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css