index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = setWeek;
  7. var _index = _interopRequireDefault(require("../getWeek/index.js"));
  8. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  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. * @param {Date|Number} date - the date to be changed
  26. * @param {Number} week - the week of the new date
  27. * @param {Object} [options] - an object with options.
  28. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  29. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  30. * @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
  31. * @returns {Date} the new date with the local week set
  32. * @throws {TypeError} 2 arguments required
  33. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  34. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  35. *
  36. * @example
  37. * // Set the 1st week to 2 January 2005 with default options:
  38. * const result = setWeek(new Date(2005, 0, 2), 1)
  39. * //=> Sun Dec 26 2004 00:00:00
  40. *
  41. * @example
  42. * // Set the 1st week to 2 January 2005,
  43. * // if Monday is the first day of the week,
  44. * // and the first week of the year always contains 4 January:
  45. * const result = setWeek(new Date(2005, 0, 2), 1, {
  46. * weekStartsOn: 1,
  47. * firstWeekContainsDate: 4
  48. * })
  49. * //=> Sun Jan 4 2004 00:00:00
  50. */
  51. function setWeek(dirtyDate, dirtyWeek, options) {
  52. (0, _index3.default)(2, arguments);
  53. var date = (0, _index2.default)(dirtyDate);
  54. var week = (0, _index4.default)(dirtyWeek);
  55. var diff = (0, _index.default)(date, options) - week;
  56. date.setDate(date.getDate() - diff * 7);
  57. return date;
  58. }
  59. module.exports = exports.default;