index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setQuarter;
  6. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../setMonth/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 setQuarter
  13. * @category Quarter Helpers
  14. * @summary Set the year quarter to the given date.
  15. *
  16. * @description
  17. * Set the year quarter to the given date.
  18. *
  19. * ### v2.0.0 breaking changes:
  20. *
  21. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  22. *
  23. * @param {Date|Number} date - the date to be changed
  24. * @param {Number} quarter - the quarter of the new date
  25. * @returns {Date} the new date with the quarter set
  26. * @throws {TypeError} 2 arguments required
  27. *
  28. * @example
  29. * // Set the 2nd quarter to 2 July 2014:
  30. * const result = setQuarter(new Date(2014, 6, 2), 2)
  31. * //=> Wed Apr 02 2014 00:00:00
  32. */
  33. function setQuarter(dirtyDate, dirtyQuarter) {
  34. (0, _index4.default)(2, arguments);
  35. var date = (0, _index2.default)(dirtyDate);
  36. var quarter = (0, _index.default)(dirtyQuarter);
  37. var oldQuarter = Math.floor(date.getMonth() / 3) + 1;
  38. var diff = quarter - oldQuarter;
  39. return (0, _index3.default)(date, date.getMonth() + diff * 3);
  40. }
  41. module.exports = exports.default;