index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setWeek;
  6. var _index = _interopRequireDefault(require("../getWeek/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * @name setWeek
  13. * @category Week Helpers
  14. * @summary Set the local week to the given date.
  15. *
  16. * @description
  17. * Set the local week to the given date, saving the weekday number.
  18. * The exact calculation depends on the values of
  19. * `options.weekStartsOn` (which is the index of the first day of the week)
  20. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  21. * the first week of the week-numbering year)
  22. *
  23. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  24. *
  25. * ### v2.0.0 breaking changes:
  26. *
  27. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  28. *
  29. * @param {Date|Number} date - the date to be changed
  30. * @param {Number} week - the week of the new date
  31. * @param {Object} [options] - an object with options.
  32. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  33. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  34. * @param {1|2|3|4|5|6|7} [options.firstWeekContainsDate=1] - the day of January, which is always in the first week of the year
  35. * @returns {Date} the new date with the local week set
  36. * @throws {TypeError} 2 arguments required
  37. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  38. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  39. *
  40. * @example
  41. * // Set the 1st week to 2 January 2005 with default options:
  42. * var result = setWeek(new Date(2005, 0, 2), 1)
  43. * //=> Sun Dec 26 2004 00:00:00
  44. *
  45. * @example
  46. * // Set the 1st week to 2 January 2005,
  47. * // if Monday is the first day of the week,
  48. * // and the first week of the year always contains 4 January:
  49. * var result = setWeek(new Date(2005, 0, 2), 1, {
  50. * weekStartsOn: 1,
  51. * firstWeekContainsDate: 4
  52. * })
  53. * //=> Sun Jan 4 2004 00:00:00
  54. */
  55. function setWeek(dirtyDate, dirtyWeek, dirtyOptions) {
  56. (0, _index4.default)(2, arguments);
  57. var date = (0, _index2.default)(dirtyDate);
  58. var week = (0, _index3.default)(dirtyWeek);
  59. var diff = (0, _index.default)(date, dirtyOptions) - week;
  60. date.setDate(date.getDate() - diff * 7);
  61. return date;
  62. }
  63. module.exports = exports.default;