How To Check If A Year Is A Leap Year Using JavaScript?

Check if a Year is a Leap Year using JavaScript

During JavaScript application development, you may get a requirement to check if a year is a leap year. This article will show you how to check if a year is a leap year using JavaScript.

Check if a year is a leap year using JavaScript

There are two solutions to check if a year is a leap year:

  • Check the divisibility of the year by 4, 100 and 400
  • Check if the year contains Feb 29

Now, we will take a closer look at each solution with specific examples for each of them.

Check the divisibility of the year by 4, 100 and 400

A year will be a leap year if one of the following two conditions is met:

  • That year is divisible by 4 and not divisible by 100.
  • That year is divisible by 400.

We have the following function:

function isLY(year) {
   return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}

We built the isLY() function to check if a year is a leap year. This function will check if a year is divisible by 100. If it is, the function will check if the year is divisible by 400. Otherwise, the function will check if the year is divisible by 4. The isLY() function will return true if the year is divisible by 400 or 4 and not divisible by 100. Otherwise, this function will return false.

Example:

// Check if a year is a leap year
function isLY(year) {
   if ((year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0)) {
      return (`${year} is a leap year`);
   } else {
      return (`${year} is not a leap year`);
   }
}

console.log(isLY(2020));
console.log(isLY(2022));

Output:

2020 is a leap year
2022 is not a leap year

Check if the year contains Feb 29

A leap year is a year where February has 29 days instead of 28, so to check if a year is a leap year, we need to check if the year has Feb 29.

To check if a year is a leap year. First, you must create a new date object using the new Date() method

Syntax:

new Date(year, month, day) creates a date object with a specified date.

Parameters: 

  • year, month, day: 3 numbers specify year, month, day.

The object representing Feb 29 takes three parameters: year (optional), month (1), and day (29). If the specified year does not include the 29th, the object will have the value Mar 1.

Example:

// Create a new date object 02/29/2020
const d1 = new Date(2020, 1, 29).toDateString();

// Create a new date object 02/29/2022
const d2 = new Date(2022, 1, 29).toDateString();

console.log(d1);
console.log(d2);

Output:

Sat Feb 29 2020
Tue Mar 01 2022

Now, we need to check if the day of the newly created date object equals 29. If it is, the year is a leap year. Here is the complete code.

Code:

// Check if a year is a leap year
function isLY(year) {

   // Create a new date object
   date = new Date(year, 1, 29);

   // Check if the day-of-date object is equal to 29
   if (date.getDate() === 29) {
      return (`${year} is a leap year`);
   } else {
      return (`${year} is not a leap year`);
   }
}

console.log(isLY(2020));
console.log(isLY(2022));

Output:

2020 is a leap year
2022 is not a leap year

Summary

This article has shown how to check if a year is a leap year using JavaScript. I hope the information in this article will be helpful to you. If you have any problems, please comment below. I will answer. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *