getDayOfYear.mjs 967 B

1234567891011121314151617181920212223242526272829303132
  1. import { differenceInCalendarDays } from "./differenceInCalendarDays.mjs";
  2. import { startOfYear } from "./startOfYear.mjs";
  3. import { toDate } from "./toDate.mjs";
  4. /**
  5. * @name getDayOfYear
  6. * @category Day Helpers
  7. * @summary Get the day of the year of the given date.
  8. *
  9. * @description
  10. * Get the day of the year of the given date.
  11. *
  12. * @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).
  13. *
  14. * @param date - The given date
  15. *
  16. * @returns The day of year
  17. *
  18. * @example
  19. * // Which day of the year is 2 July 2014?
  20. * const result = getDayOfYear(new Date(2014, 6, 2))
  21. * //=> 183
  22. */
  23. export function getDayOfYear(date) {
  24. const _date = toDate(date);
  25. const diff = differenceInCalendarDays(_date, startOfYear(_date));
  26. const dayOfYear = diff + 1;
  27. return dayOfYear;
  28. }
  29. // Fallback for modularized imports:
  30. export default getDayOfYear;