getDaysInYear.mjs 911 B

12345678910111213141516171819202122232425262728293031323334
  1. import { isLeapYear } from "./isLeapYear.mjs";
  2. import { toDate } from "./toDate.mjs";
  3. /**
  4. * @name getDaysInYear
  5. * @category Year Helpers
  6. * @summary Get the number of days in a year of the given date.
  7. *
  8. * @description
  9. * Get the number of days in a year of the given date.
  10. *
  11. * @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).
  12. *
  13. * @param date - The given date
  14. *
  15. * @returns The number of days in a year
  16. *
  17. * @example
  18. * // How many days are in 2012?
  19. * const result = getDaysInYear(new Date(2012, 0, 1))
  20. * //=> 366
  21. */
  22. export function getDaysInYear(date) {
  23. const _date = toDate(date);
  24. if (String(new Date(_date)) === "Invalid Date") {
  25. return NaN;
  26. }
  27. return isLeapYear(_date) ? 366 : 365;
  28. }
  29. // Fallback for modularized imports:
  30. export default getDaysInYear;