getDaysInYear.js 907 B

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. exports.getDaysInYear = getDaysInYear;
  3. var _index = require("./isLeapYear.js");
  4. var _index2 = require("./toDate.js");
  5. /**
  6. * @name getDaysInYear
  7. * @category Year Helpers
  8. * @summary Get the number of days in a year of the given date.
  9. *
  10. * @description
  11. * Get the number of days in a year of the given date.
  12. *
  13. * @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).
  14. *
  15. * @param date - The given date
  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. function getDaysInYear(date) {
  25. const _date = (0, _index2.toDate)(date);
  26. if (String(new Date(_date)) === "Invalid Date") {
  27. return NaN;
  28. }
  29. return (0, _index.isLeapYear)(_date) ? 366 : 365;
  30. }