index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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. * @param {Date|Number} date - the date to be changed
  17. * @param {Number} day - the day of the ISO week of the new date
  18. * @returns {Date} the new date with the day of the ISO week set
  19. * @throws {TypeError} 2 arguments required
  20. *
  21. * @example
  22. * // Set Sunday to 1 September 2014:
  23. * const result = setISODay(new Date(2014, 8, 1), 7)
  24. * //=> Sun Sep 07 2014 00:00:00
  25. */
  26. export default function setISODay(dirtyDate, dirtyDay) {
  27. requiredArgs(2, arguments);
  28. var date = toDate(dirtyDate);
  29. var day = toInteger(dirtyDay);
  30. var currentDay = getISODay(date);
  31. var diff = day - currentDay;
  32. return addDays(date, diff);
  33. }