index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = setQuarter;
  7. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  8. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index3 = _interopRequireDefault(require("../setMonth/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  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. * @param {Date|Number} date - the date to be changed
  20. * @param {Number} quarter - the quarter of the new date
  21. * @returns {Date} the new date with the quarter set
  22. * @throws {TypeError} 2 arguments required
  23. *
  24. * @example
  25. * // Set the 2nd quarter to 2 July 2014:
  26. * const result = setQuarter(new Date(2014, 6, 2), 2)
  27. * //=> Wed Apr 02 2014 00:00:00
  28. */
  29. function setQuarter(dirtyDate, dirtyQuarter) {
  30. (0, _index4.default)(2, arguments);
  31. var date = (0, _index2.default)(dirtyDate);
  32. var quarter = (0, _index.default)(dirtyQuarter);
  33. var oldQuarter = Math.floor(date.getMonth() / 3) + 1;
  34. var diff = quarter - oldQuarter;
  35. return (0, _index3.default)(date, date.getMonth() + diff * 3);
  36. }
  37. module.exports = exports.default;