index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import toInteger from "../_lib/toInteger/index.js";
  2. import toDate from "../toDate/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.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. * ### v2.0.0 breaking changes:
  13. *
  14. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  15. *
  16. * @param {Date|Number} date - the date to be changed
  17. * @param {Number} dayOfYear - the day of the year of the new date
  18. * @returns {Date} the new date with the day of the year set
  19. * @throws {TypeError} 2 arguments required
  20. *
  21. * @example
  22. * // Set the 2nd day of the year to 2 July 2014:
  23. * var result = setDayOfYear(new Date(2014, 6, 2), 2)
  24. * //=> Thu Jan 02 2014 00:00:00
  25. */
  26. export default function setDayOfYear(dirtyDate, dirtyDayOfYear) {
  27. requiredArgs(2, arguments);
  28. var date = toDate(dirtyDate);
  29. var dayOfYear = toInteger(dirtyDayOfYear);
  30. date.setMonth(0);
  31. date.setDate(dayOfYear);
  32. return date;
  33. }