fdu logo@pyyupsk/fdu - Faster Date-Time Utility
API ReferenceQuery

isLeapYear()

Check if the year of a date is a leap year using proper Gregorian calendar rules. Essential for date validation, calendar applications, and age calculations.

Checks if the year of this date is a leap year according to the Gregorian calendar rules.

Syntax

.isLeapYear(): boolean

Returns

boolean - True if the year is a leap year

Leap Year Rules

A year is a leap year if:

  1. It is divisible by 4, AND
  2. Either not divisible by 100, OR divisible by 400

Examples:

  • 2024 is a leap year (divisible by 4)
  • 1900 is NOT a leap year (divisible by 100 but not 400)
  • 2000 IS a leap year (divisible by 400)

Examples

Basic Usage

import { fdu } from "@pyyupsk/fdu";

fdu("2024-01-15").isLeapYear(); // true
fdu("2023-01-15").isLeapYear(); // false
fdu("2000-01-15").isLeapYear(); // true
fdu("1900-01-15").isLeapYear(); // false

February Days Check

function getDaysInFebruary(year: number): number {
  return fdu({ year, month: 1, day: 1 }).isLeapYear() ? 29 : 28;
}

getDaysInFebruary(2024); // 29
getDaysInFebruary(2023); // 28

Date Validation

function isValidDate(year: number, month: number, day: number): boolean {
  const date = fdu({ year, month, day });

  if (month === 1 && day === 29) {
    return date.isLeapYear();
  }

  return date.isValid();
}

isValidDate(2024, 1, 29); // true (Feb 29, 2024 exists)
isValidDate(2023, 1, 29); // false (Feb 29, 2023 doesn't exist)

Next Leap Year

function nextLeapYear(date: FduInstance): number {
  let year = date.year();

  while (!fdu({ year, month: 0, day: 1 }).isLeapYear()) {
    year++;
  }

  return year;
}

nextLeapYear(fdu("2023-06-15")); // 2024
nextLeapYear(fdu("2024-06-15")); // 2024
nextLeapYear(fdu("2025-01-01")); // 2028

Leap Year Birthday Handling

function getNextBirthday(birthdate: FduInstance): FduInstance {
  const today = fdu();
  let targetYear = today.year();

  // Start with this year's birthday
  let nextBirthday = fdu({
    year: targetYear,
    month: birthdate.month(),
    day: birthdate.date(),
  });

  // If it already passed, move to next year
  if (nextBirthday.isBefore(today)) {
    targetYear += 1;
  }

  // Handle Feb 29 birthdays specially
  if (birthdate.month() === 1 && birthdate.date() === 29) {
    const isLeap = fdu({ year: targetYear, month: 0, day: 1 }).isLeapYear();
    const day = isLeap ? 29 : 28;
    return fdu({ year: targetYear, month: 1, day });
  }

  // All other birthdays
  return fdu({
    year: targetYear,
    month: birthdate.month(),
    day: birthdate.date(),
  });
}

See Also

On this page