isLeapYear.mjs 824 B

1234567891011121314151617181920212223242526272829
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name isLeapYear
  4. * @category Year Helpers
  5. * @summary Is the given date in the leap year?
  6. *
  7. * @description
  8. * Is the given date in the leap year?
  9. *
  10. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  11. *
  12. * @param date - The date to check
  13. *
  14. * @returns The date is in the leap year
  15. *
  16. * @example
  17. * // Is 1 September 2012 in the leap year?
  18. * const result = isLeapYear(new Date(2012, 8, 1))
  19. * //=> true
  20. */
  21. export function isLeapYear(date) {
  22. const _date = toDate(date);
  23. const year = _date.getFullYear();
  24. return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
  25. }
  26. // Fallback for modularized imports:
  27. export default isLeapYear;