Skip to content
Related Articles

Related Articles

Improve Article
Filtering Weekdays and Weekends using Vue filters
  • Last Updated : 31 Mar, 2021

Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries.

Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is the one that’s actually printed in the Vue.js template. 

To filter out weekdays and weekends in a week we have to write the filtering logic based on workdays and holidays. We use a switch-case for this and filter based on the names of the days.

Example:

index.html






<html>
<head>
  <script src=
  </script>
</head>
<body>
  <h1 style="color: green;">
    GeeksforGeeks
  </h1>
  <div id='parent'>
    <p><strong>{{day1}} : </strong
      {{ day1 | weeks }}
    </p>
  
    <p><strong>{{day2}} : </strong
      {{ day2 | weeks }}
    </p>
  
    <p><strong>{{day3}} : </strong>
      {{ day3 | weeks }}
    </p>
  
    <p><strong>{{day4}} : </strong>
      {{ day4 | weeks }}
    </p>
  
    <p><strong>{{day5}} : </strong>
      {{ day5 | weeks }}
    </p>
  
  </div>
  <script src='app.js'></script>
</body>
</html>

app.js




const parent = new Vue({
  el: "#parent",
  data: {
    day1: "Thursday",
    day2: "Tuesday",
    day3: "Sunday",
    day4: "Monday",
    day5: "Saturday",
  },
  
  filters: {
    weeks: function (day) {
      
      // Using switch case to
      // decide between the days
      switch (day) {
        case "Monday":
        case "Tuesday":
        case "Wednesday":
        case "Thursday":
        case "Friday":
          return "It is Weekday, We have to work.";
        case "Saturday":
        case "Sunday":
          return "It is Weekend, We do not have to work.";
      }
    },
  },
});

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :