index.js 1.1 KB

123456789101112131415161718192021222324
  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 startOfUTCWeek(dirtyDate, dirtyOptions) {
  6. requiredArgs(1, 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 = date.getUTCDay();
  17. var diff = (day < weekStartsOn ? 7 : 0) + day - weekStartsOn;
  18. date.setUTCDate(date.getUTCDate() - diff);
  19. date.setUTCHours(0, 0, 0, 0);
  20. return date;
  21. }