Cool way to check for leap years in JavaScript
Published on in JavaScript
Spoiler:
new Date(year, 1, 29).getDate() === 29
.
I stumbled upon a tweet by Colin DeCarlo about a cool way to check if a given year is a leap year (code by JleCoRyl3):
function isLearYear(year) {
return new Date(year, 1, 29).getDate() === 29
}
Quite clever! Though a bit confusing at first because months are 0-indexed. Maybe a code comment would be in order.
The tweet is what inspired me to start doing TypeScript exercises on Exercism.