index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setISOWeek;
  6. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../getISOWeek/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 setISOWeek
  13. * @category ISO Week Helpers
  14. * @summary Set the ISO week to the given date.
  15. *
  16. * @description
  17. * Set the ISO week to the given date, saving the weekday number.
  18. *
  19. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  20. *
  21. * ### v2.0.0 breaking changes:
  22. *
  23. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  24. *
  25. * @param {Date|Number} date - the date to be changed
  26. * @param {Number} isoWeek - the ISO week of the new date
  27. * @returns {Date} the new date with the ISO week set
  28. * @throws {TypeError} 2 arguments required
  29. *
  30. * @example
  31. * // Set the 53rd ISO week to 7 August 2004:
  32. * const result = setISOWeek(new Date(2004, 7, 7), 53)
  33. * //=> Sat Jan 01 2005 00:00:00
  34. */
  35. function setISOWeek(dirtyDate, dirtyISOWeek) {
  36. (0, _index4.default)(2, arguments);
  37. var date = (0, _index2.default)(dirtyDate);
  38. var isoWeek = (0, _index.default)(dirtyISOWeek);
  39. var diff = (0, _index3.default)(date) - isoWeek;
  40. date.setDate(date.getDate() - diff * 7);
  41. return date;
  42. }
  43. module.exports = exports.default;