index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = setISOWeek;
  7. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  8. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index3 = _interopRequireDefault(require("../getISOWeek/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  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. * @param {Date|Number} date - the date to be changed
  22. * @param {Number} isoWeek - the ISO week of the new date
  23. * @returns {Date} the new date with the ISO week set
  24. * @throws {TypeError} 2 arguments required
  25. *
  26. * @example
  27. * // Set the 53rd ISO week to 7 August 2004:
  28. * const result = setISOWeek(new Date(2004, 7, 7), 53)
  29. * //=> Sat Jan 01 2005 00:00:00
  30. */
  31. function setISOWeek(dirtyDate, dirtyISOWeek) {
  32. (0, _index4.default)(2, arguments);
  33. var date = (0, _index2.default)(dirtyDate);
  34. var isoWeek = (0, _index.default)(dirtyISOWeek);
  35. var diff = (0, _index3.default)(date) - isoWeek;
  36. date.setDate(date.getDate() - diff * 7);
  37. return date;
  38. }
  39. module.exports = exports.default;