index.js 914 B

123456789101112131415161718192021222324252627282930
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name isLeapYear
  5. * @category Year Helpers
  6. * @summary Is the given date in the leap year?
  7. *
  8. * @description
  9. * Is the given date in the leap year?
  10. *
  11. * ### v2.0.0 breaking changes:
  12. *
  13. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  14. *
  15. * @param {Date|Number} date - the date to check
  16. * @returns {Boolean} the date is in the leap year
  17. * @throws {TypeError} 1 argument required
  18. *
  19. * @example
  20. * // Is 1 September 2012 in the leap year?
  21. * var result = isLeapYear(new Date(2012, 8, 1))
  22. * //=> true
  23. */
  24. export default function isLeapYear(dirtyDate) {
  25. requiredArgs(1, arguments);
  26. var date = toDate(dirtyDate);
  27. var year = date.getFullYear();
  28. return year % 400 === 0 || year % 4 === 0 && year % 100 !== 0;
  29. }