isLeapYear

JavaScript, Date

Checks if the given year is a leap year.

  • Use new Date(), setting the date to February 29th of the given year.
  • Use Date.prototype.getMonth() to check if the month is equal to 1.
const isLeapYear = year => new Date(year, 1, 29).getMonth() === 1;
isLeapYear(2019); // false
isLeapYear(2020); // true

Recommended snippets

  • isDateValid

    JavaScript, Date

    Checks if a valid date object can be created from the given values.

  • isISOString

    JavaScript, Date

    Checks if the given string is valid in the simplified extended ISO format (ISO 8601).

  • daysInMonth

    JavaScript, Date

    Gets the number of days in the given month of the specified year.