Javascript Data Type How to - Check if a year is leap year








Question

We would like to know how to check if a year is leap year.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
    function isleap(year) {<!-- w  w w .ja  va  2s.  c om-->
        year = parseInt(year,10);
        if (isNaN(year)) {
           document.writeln("Not a number");
           return false;
        }
        if (year%4===0 && (year%100!=0 || year%400===0)) {
           document.writeln("Leap");
           return true;
        } else {
           document.writeln("Not leap");
           return false;
        }
    }

</script>
</head>
<body>
  <button onclick="isleap(1992)">Try it</button>
</body>
</html>

The code above is rendered as follows: