index.js 823 B

12345678910111213141516171819202122232425262728
  1. import toDate from "../toDate/index.js";
  2. import isLeapYear from "../isLeapYear/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  4. /**
  5. * @name getDaysInYear
  6. * @category Year Helpers
  7. * @summary Get the number of days in a year of the given date.
  8. *
  9. * @description
  10. * Get the number of days in a year of the given date.
  11. *
  12. * @param {Date|Number} date - the given date
  13. * @returns {Number} the number of days in a year
  14. * @throws {TypeError} 1 argument required
  15. *
  16. * @example
  17. * // How many days are in 2012?
  18. * const result = getDaysInYear(new Date(2012, 0, 1))
  19. * //=> 366
  20. */
  21. export default function getDaysInYear(dirtyDate) {
  22. requiredArgs(1, arguments);
  23. var date = toDate(dirtyDate);
  24. if (String(new Date(date)) === 'Invalid Date') {
  25. return NaN;
  26. }
  27. return isLeapYear(date) ? 366 : 365;
  28. }