setDayOfYear.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link setDayOfYear} function options.
  4. */
  5. /**
  6. * @name setDayOfYear
  7. * @category Day Helpers
  8. * @summary Set the day of the year to the given date.
  9. *
  10. * @description
  11. * Set the day of the year to 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. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  15. *
  16. * @param date - The date to be changed
  17. * @param dayOfYear - The day of the year of the new date
  18. * @param options - An object with options
  19. *
  20. * @returns The new date with the day of the year set
  21. *
  22. * @example
  23. * // Set the 2nd day of the year to 2 July 2014:
  24. * const result = setDayOfYear(new Date(2014, 6, 2), 2)
  25. * //=> Thu Jan 02 2014 00:00:00
  26. */
  27. export function setDayOfYear(date, dayOfYear, options) {
  28. const date_ = toDate(date, options?.in);
  29. date_.setMonth(0);
  30. date_.setDate(dayOfYear);
  31. return date_;
  32. }
  33. // Fallback for modularized imports:
  34. export default setDayOfYear;