index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setISOWeekYear;
  6. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../startOfISOWeekYear/index.js"));
  9. var _index4 = _interopRequireDefault(require("../differenceInCalendarDays/index.js"));
  10. var _index5 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * @name setISOWeekYear
  14. * @category ISO Week-Numbering Year Helpers
  15. * @summary Set the ISO week-numbering year to the given date.
  16. *
  17. * @description
  18. * Set the ISO week-numbering year to the given date,
  19. * saving the week number and the weekday number.
  20. *
  21. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  22. *
  23. * ### v2.0.0 breaking changes:
  24. *
  25. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  26. *
  27. * - The function was renamed from `setISOYear` to `setISOWeekYear`.
  28. * "ISO week year" is short for [ISO week-numbering year](https://en.wikipedia.org/wiki/ISO_week_date).
  29. * This change makes the name consistent with
  30. * locale-dependent week-numbering year helpers, e.g., `setWeekYear`.
  31. *
  32. * @param {Date|Number} date - the date to be changed
  33. * @param {Number} isoWeekYear - the ISO week-numbering year of the new date
  34. * @returns {Date} the new date with the ISO week-numbering year set
  35. * @throws {TypeError} 2 arguments required
  36. *
  37. * @example
  38. * // Set ISO week-numbering year 2007 to 29 December 2008:
  39. * const result = setISOWeekYear(new Date(2008, 11, 29), 2007)
  40. * //=> Mon Jan 01 2007 00:00:00
  41. */
  42. function setISOWeekYear(dirtyDate, dirtyISOWeekYear) {
  43. (0, _index5.default)(2, arguments);
  44. var date = (0, _index2.default)(dirtyDate);
  45. var isoWeekYear = (0, _index.default)(dirtyISOWeekYear);
  46. var diff = (0, _index4.default)(date, (0, _index3.default)(date));
  47. var fourthOfJanuary = new Date(0);
  48. fourthOfJanuary.setFullYear(isoWeekYear, 0, 4);
  49. fourthOfJanuary.setHours(0, 0, 0, 0);
  50. date = (0, _index3.default)(fourthOfJanuary);
  51. date.setDate(date.getDate() + diff);
  52. return date;
  53. }
  54. module.exports = exports.default;