Open In App

Find Leapyear and Non-Leapyear using Vue filters

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Approach: The filter logic would first check if the required year is divisible by 100 or not. If it is divisible, then we will also have to check its divisibility with 400. If divisible by both, then the specified year is a leap year. If the year is not divisible by 100 itself, then we only need to check if it is divisible by 4. If it is divisible then the year is a leap year, otherwise, it is not a leap year.

Example:

index.html




<html>
<head>
    <script src=
    </script>
</head>
<body>
  <h1 style="color: green;">
    GeeksforGeeks
  </h1>
    <div id='parent'>
        <p><strong>{{year1}} : </strong
          {{ year1 | leapyear }}
        </p>
  
        <p><strong>{{year2}} : </strong>
          {{ year2 | leapyear }}
        </p>
  
        <p><strong>{{year3}} : </strong>
           {{ year3 | leapyear }}
        </p>
  
        <p><strong>{{year4}} : </strong>
          {{ year4 | leapyear }}
        </p>
  
        <p><strong>{{year5}} : </strong>
          {{ year5 | leapyear }}
        </p>
  
    </div>
    <script src='app.js'></script>
</body>
</html>


app.js




const parent = new Vue({
  el: "#parent",
  data: {
    year1: 2004,
    year2: 1996,
    year3: 1900,
    year4: 2010,
    year5: 1960,
  },
  
  filters: {
    leapyear: function (year) {
      if (year % 100 === 0) {
        if (year % 400 === 0) {
          return "Leapyear";
        } else {
          return "Non-Leapyear";
        }
      } else {
        if (year % 4 === 0) {
          return "Leapyear";
        } else {
          return "Non-Leapyear";
        }
      }
    },
  },
});


Output:



Last Updated : 31 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads