1234567891011121314151617181920212223242526 |
- import toInteger from "../toInteger/index.js";
- import toDate from "../../toDate/index.js";
- import requiredArgs from "../requiredArgs/index.js"; // This function will be a part of public API when UTC function will be implemented.
- // See issue: https://github.com/date-fns/date-fns/issues/376
- export default function setUTCDay(dirtyDate, dirtyDay, dirtyOptions) {
- requiredArgs(2, arguments);
- var options = dirtyOptions || {};
- var locale = options.locale;
- var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn;
- var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : toInteger(localeWeekStartsOn);
- var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : toInteger(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
- if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
- throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
- }
- var date = toDate(dirtyDate);
- var day = toInteger(dirtyDay);
- var currentDay = date.getUTCDay();
- var remainder = day % 7;
- var dayIndex = (remainder + 7) % 7;
- var diff = (dayIndex < weekStartsOn ? 7 : 0) + day - currentDay;
- date.setUTCDate(date.getUTCDate() + diff);
- return date;
- }
|