index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import addDays from "../addDays/index.js";
  2. import toDate from "../toDate/index.js";
  3. import toInteger from "../_lib/toInteger/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
  6. /**
  7. * @name setDay
  8. * @category Weekday Helpers
  9. * @summary Set the day of the week to the given date.
  10. *
  11. * @description
  12. * Set the day of the week to the given date.
  13. *
  14. * @param {Date|Number} date - the date to be changed
  15. * @param {Number} day - the day of the week of the new date
  16. * @param {Object} [options] - an object with options.
  17. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  18. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  19. * @returns {Date} the new date with the day of the week set
  20. * @throws {TypeError} 2 arguments required
  21. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  22. *
  23. * @example
  24. * // Set week day to Sunday, with the default weekStartsOn of Sunday:
  25. * const result = setDay(new Date(2014, 8, 1), 0)
  26. * //=> Sun Aug 31 2014 00:00:00
  27. *
  28. * @example
  29. * // Set week day to Sunday, with a weekStartsOn of Monday:
  30. * const result = setDay(new Date(2014, 8, 1), 0, { weekStartsOn: 1 })
  31. * //=> Sun Sep 07 2014 00:00:00
  32. */
  33. export default function setDay(dirtyDate, dirtyDay, options) {
  34. var _ref, _ref2, _ref3, _options$weekStartsOn, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  35. requiredArgs(2, arguments);
  36. var defaultOptions = getDefaultOptions();
  37. var weekStartsOn = toInteger((_ref = (_ref2 = (_ref3 = (_options$weekStartsOn = options === null || options === void 0 ? void 0 : options.weekStartsOn) !== null && _options$weekStartsOn !== void 0 ? _options$weekStartsOn : options === null || options === void 0 ? void 0 : (_options$locale = options.locale) === null || _options$locale === void 0 ? void 0 : (_options$locale$optio = _options$locale.options) === null || _options$locale$optio === void 0 ? void 0 : _options$locale$optio.weekStartsOn) !== null && _ref3 !== void 0 ? _ref3 : defaultOptions.weekStartsOn) !== null && _ref2 !== void 0 ? _ref2 : (_defaultOptions$local = defaultOptions.locale) === null || _defaultOptions$local === void 0 ? void 0 : (_defaultOptions$local2 = _defaultOptions$local.options) === null || _defaultOptions$local2 === void 0 ? void 0 : _defaultOptions$local2.weekStartsOn) !== null && _ref !== void 0 ? _ref : 0);
  38. // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  39. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  40. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  41. }
  42. var date = toDate(dirtyDate);
  43. var day = toInteger(dirtyDay);
  44. var currentDay = date.getDay();
  45. var remainder = day % 7;
  46. var dayIndex = (remainder + 7) % 7;
  47. var delta = 7 - weekStartsOn;
  48. var diff = day < 0 || day > 6 ? day - (currentDay + delta) % 7 : (dayIndex + delta) % 7 - (currentDay + delta) % 7;
  49. return addDays(date, diff);
  50. }