getDayOfYear.js 889 B

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