How to determine if date is weekend in JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

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
raja
Published on 03-Sep-2020 11:02:27
Advertisements