getDaysInYear.js 812 B

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