DEV Community

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

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.