index.js 749 B

12345678910111213141516171819202122
  1. import toInteger from "../toInteger/index.js";
  2. import toDate from "../../toDate/index.js";
  3. import requiredArgs from "../requiredArgs/index.js"; // This function will be a part of public API when UTC function will be implemented.
  4. // See issue: https://github.com/date-fns/date-fns/issues/376
  5. export default function setUTCISODay(dirtyDate, dirtyDay) {
  6. requiredArgs(2, arguments);
  7. var day = toInteger(dirtyDay);
  8. if (day % 7 === 0) {
  9. day = day - 7;
  10. }
  11. var weekStartsOn = 1;
  12. var date = toDate(dirtyDate);
  13. var currentDay = date.getUTCDay();
  14. var remainder = day % 7;
  15. var dayIndex = (remainder + 7) % 7;
  16. var diff = (dayIndex < weekStartsOn ? 7 : 0) + day - currentDay;
  17. date.setUTCDate(date.getUTCDate() + diff);
  18. return date;
  19. }