index.js 1.2 KB

1234567891011121314151617181920212223242526
  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 setUTCDay(dirtyDate, dirtyDay, dirtyOptions) {
  6. requiredArgs(2, arguments);
  7. var options = dirtyOptions || {};
  8. var locale = options.locale;
  9. var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn;
  10. var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : toInteger(localeWeekStartsOn);
  11. var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : toInteger(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  12. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  13. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  14. }
  15. var date = toDate(dirtyDate);
  16. var day = toInteger(dirtyDay);
  17. var currentDay = date.getUTCDay();
  18. var remainder = day % 7;
  19. var dayIndex = (remainder + 7) % 7;
  20. var diff = (dayIndex < weekStartsOn ? 7 : 0) + day - currentDay;
  21. date.setUTCDate(date.getUTCDate() + diff);
  22. return date;
  23. }