HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Check if a year is a leap year in JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
leapjavascriptyearcheck

Problem

A leap year is a year that is evenly divisible by 4, except for end-of-century years, which must be divisible by 400.
While detecting a leap year via code sounds like a lot of work, there's a method that doesn't really involve any math whatsoever. Using the Date object, you can easily check if a given year is a leap year.
All you have to do is use the Date() constructor to create an object for February 29th of the given year. If the month is February, then Date.prototye.getMonth() will return 1 (since months are zero-based in JavaScript), meaning it's a leap year. If the month is not February, the date will be set to March 1st, which means it's not a leap year.

Solution

const isLeapYear = year => new Date(year, 1, 29).getMonth() === 1;

isLeapYear(2019); // false
isLeapYear(2020); // true


All you have to do is use the Date() constructor to create an object for February 29th of the given year. If the month is February, then Date.prototye.getMonth() will return 1 (since months are zero-based in JavaScript), meaning it's a leap year. If the month is not February, the date will be set to March 1st, which means it's not a leap year.

Code Snippets

const isLeapYear = year => new Date(year, 1, 29).getMonth() === 1;

isLeapYear(2019); // false
isLeapYear(2020); // true

Context

From 30-seconds-of-code: is-leap-year

Revisions (0)

No revisions yet.