How To Check Date Is Weekend In JavaScript Tutorial

Reading Time: 4 minutes
2,664 Views

Inside this article we will see how to check date is weekend in javascript or not. If you are creating any application in which you are managing employees salaries etc then you may want to any date is a weekend date or not then this article concept will help you.

If you are looking for an article which gives you understanding about finding a date is weekend or not in javascript then you are at the right place to learn and implement it.

In this classified piece of information we will see how to check a date is weekend date or not in javascript. We will understand by two examples in this article.

Learn More –

Let’s get started.


Create an Application

Example #1

Create a folder with name weekend-date. Create a file index.html into it.

Open index.html and write this complete code into it.

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How To Check Date Is Weekend In JavaScript Tutorial</title>
</head>

<body>
    <h3>How To Check Date Is Weekend In JavaScript Tutorial</h3>

    <script type="text/javascript">
        var d = new Date();
        var n = d.getDay();

        // 6 = Saturday, 0 = Sunday

        if (n == 6 || n == 0) {
            console.log("It's weekend!");
        } else {
            console.log("It's not weekend");
        }
    </script>
</body>

</html>

Concept

  • Using Date() method
  • Find day number
  • if day equals 6 then Saturday also if 0 then Sunday

Currently here it’s 11 Dec 2021, so according to this date

Output

In console window you will get It’s weekend!

Example #2

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How To Check Date Is Weekend In JavaScript Tutorial</title>
</head>

<body>
    <h3>How To Check Date Is Weekend In JavaScript Tutorial</h3>

    <script type="text/javascript">
        var givenDate = new Date('2021-12-11'); // yy/mm/dd format
        var day = givenDate.getDay();  // 6 = Saturday, 0 = Sunday
        var isWeekend = (day === 6) || (day === 0) ? 'This is Weekend' : 'This is Working Day';

        console.log(isWeekend);
    </script>
</body>

</html>
      

Output

This is Weekend

We hope this article helped you to learn How To Check Date Is Weekend In JavaScript Tutorial Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.