index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import toInteger from "../_lib/toInteger/index.js";
  2. import toDate from "../toDate/index.js";
  3. import addDays from "../addDays/index.js";
  4. import getISODay from "../getISODay/index.js";
  5. import requiredArgs from "../_lib/requiredArgs/index.js";
  6. /**
  7. * @name setISODay
  8. * @category Weekday Helpers
  9. * @summary Set the day of the ISO week to the given date.
  10. *
  11. * @description
  12. * Set the day of the ISO week to the given date.
  13. * ISO week starts with Monday.
  14. * 7 is the index of Sunday, 1 is the index of Monday etc.
  15. *
  16. * ### v2.0.0 breaking changes:
  17. *
  18. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  19. *
  20. * @param {Date|Number} date - the date to be changed
  21. * @param {Number} day - the day of the ISO week of the new date
  22. * @returns {Date} the new date with the day of the ISO week set
  23. * @throws {TypeError} 2 arguments required
  24. *
  25. * @example
  26. * // Set Sunday to 1 September 2014:
  27. * const result = setISODay(new Date(2014, 8, 1), 7)
  28. * //=> Sun Sep 07 2014 00:00:00
  29. */
  30. export default function setISODay(dirtyDate, dirtyDay) {
  31. requiredArgs(2, arguments);
  32. var date = toDate(dirtyDate);
  33. var day = toInteger(dirtyDay);
  34. var currentDay = getISODay(date);
  35. var diff = day - currentDay;
  36. return addDays(date, diff);
  37. }