- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to determine if date is weekend in JavaScript?
As we know the value 0 is for day Sunday and 6 for Saturday. First of all, you need to get day with the help of getDay().
Let’s set a date −
var givenDate=new Date("2020-07-18");
Now, we will get the day −
var currentDay = givenDate.getDay();
Following is the code to determine if date is weekend −
Example
var givenDate=new Date("2020-07-18"); var currentDay = givenDate.getDay(); var dateIsInWeekend = (currentDay === 6) || (currentDay === 0); if(dateIsInWeekend==true){ console.log("The given date "+givenDate+" is a Weekend"); } else { console.log("The given date " +givenDate+"is a not a Weekend"); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo66.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo66.js The given date Sat Jul 18 2020 05:30:00 GMT+0530 (India Standard Time) is a Weekend
Advertisements