setDayOfYear.js 910 B

123456789101112131415161718192021222324252627282930
  1. "use strict";
  2. exports.setDayOfYear = setDayOfYear;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name setDayOfYear
  6. * @category Day Helpers
  7. * @summary Set the day of the year to the given date.
  8. *
  9. * @description
  10. * Set the day of the year to 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 date to be changed
  15. * @param dayOfYear - The day of the year of the new date
  16. *
  17. * @returns The new date with the day of the year set
  18. *
  19. * @example
  20. * // Set the 2nd day of the year to 2 July 2014:
  21. * const result = setDayOfYear(new Date(2014, 6, 2), 2)
  22. * //=> Thu Jan 02 2014 00:00:00
  23. */
  24. function setDayOfYear(date, dayOfYear) {
  25. const _date = (0, _index.toDate)(date);
  26. _date.setMonth(0);
  27. _date.setDate(dayOfYear);
  28. return _date;
  29. }