DEV Community

loading...
Cover image for Creating a JavaScript Function to Calculate Whether It's a Leap Year

Creating a JavaScript Function to Calculate Whether It's a Leap Year

Nick Scialli (he/him)
Husband, dog dad, software engineer, coffee monster. Working in civic tech!
・1 min read

Calculating whether it's a leap year isn't a straightforward as you might think! Here's how leap years are calculated, as described on Wikipedia:

In the Gregorian calendar, each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28. These extra days occur in each year which is an integer multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400).

Wat? 🤯

Let's break this down into enumerable steps, which we can then convert into code:

Note: This is an academic exercise! If you're going to do any date calculations in a production application, I'd strongly encourage you to use a tried-and-true library like moment.js. Dates can be tricky and you don't want to hit nasty bugs rolling your own solution!

1) If a year is divisible by 400, it's a leap year
2) Otherwise, if a year is divisible by 100, it's not a leap year
3) Otherwise, if a year is divisible by 4, it's a leap year

This is fairly straightforward now and can be converted into code:

function isLeapYear(year) {
  if (year % 400 === 0) return true;
  if (year % 100 === 0) return false;
  return year % 4 === 0;
}

And we can test a few scenarios:

isLeapYear(2000) // true
isLeapYear(2001) // false
isLeapYear(2004) // true
isLeapYear(2100) // false

Happy coding!

Discussion (15)

Collapse
lucs1590 profile image
Lucas de Brito • Edited

You can do a ternary condition to minimize lines, like this:

function isLeapYear(year) {
    return true ? ((year % 400 === 0) || (year % 100 !== 0)) && ((year % 4) == 0) : false;
};
Collapse
nas5w profile image
Nick Scialli (he/him) Author

I don't think this will evaluate correctly. Also, long one-liners can feel efficient but they're often pretty hard for others to understand.

Collapse
lucs1590 profile image
Lucas de Brito

Really, for those who are not used to it, it is difficult to understand. When I started working with tender conditions I thought "This is very strange!", but I forced myself to learn (even to leave the comfort zone) and now it has become very simple. It's a matter of habit, and believe me, it works perfectly!

Thread Thread
nas5w profile image
Nick Scialli (he/him) Author • Edited

What I'm saying is that your function literally doesn't work correctly.

isLeapYear(2100);
// true

That's incorrect.

Thread Thread
lucs1590 profile image
Lucas de Brito

Oh, sorry, you're right! I forget something.
Try again with my changes.

Thread Thread
nas5w profile image
Nick Scialli (he/him) Author

In this case, why do you even need the ternary? true will always evaluate to true.

In other words, this:

true ? ((year % 400 === 0) || (year % 100 !== 0)) && ((year % 4) == 0) : false;

is the exact same thing as this:

((year % 400 === 0) || (year % 100 !== 0)) && ((year % 4) == 0);
Thread Thread
lucs1590 profile image
Lucas de Brito

Perfect!!
I totally agree with you! 👏👏👏👏👏👏

Collapse
hanpari profile image
Pavel Morava

Or just write

isLeapYear = (year) => (new Date(year, 1, 29)).getDate() === 29

The longest part to write this involved searching how to get the day of a month in Javascript.

Seriously, getDate instead getDay!!!

And of course, counting months from 0, so I needed to use 1 for February. What an incredible language!

Collapse
craigewert profile image
CREEE

Clever, but too obscure to save 2 lines of code.

Moreover, using date libs is not cricket. If you have those, I presume you can just ask

Date.isLeapYear(year)

(with some syntax or other)

Collapse
hanpari profile image
Pavel Morava

Is not cricket?

I had to google this one. It made me laugh when I learned what it means.

Frankly, it was a quick idea, surprisingly hard to code, given Javascript odd standard library.

Collapse
generalkorex profile image
Korex

Wow this is straight forward. Thanks for sharing.

Collapse
upieez profile image
Samuel Huang

I love these short questions and simple explanations! Please do more of these 😁

Collapse
craigewert profile image
CREEE

Very clean. But do the 3 parts in the opposite order.

Assuming you get years randomly, most will be not %4, so handle those first.
Similarly, most %4 years will be not %100, so those are next.

just in case the price of 2 extra "if"s is going to break you.

Collapse
thomasbnt profile image
Thomas Bnt

Oh great :o