setQuarter.js 1009 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. exports.setQuarter = setQuarter;
  3. var _index = require("./setMonth.js");
  4. var _index2 = require("./toDate.js");
  5. /**
  6. * @name setQuarter
  7. * @category Quarter Helpers
  8. * @summary Set the year quarter to the given date.
  9. *
  10. * @description
  11. * Set the year quarter to the given date.
  12. *
  13. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  14. *
  15. * @param date - The date to be changed
  16. * @param quarter - The quarter of the new date
  17. *
  18. * @returns The new date with the quarter set
  19. *
  20. * @example
  21. * // Set the 2nd quarter to 2 July 2014:
  22. * const result = setQuarter(new Date(2014, 6, 2), 2)
  23. * //=> Wed Apr 02 2014 00:00:00
  24. */
  25. function setQuarter(date, quarter) {
  26. const _date = (0, _index2.toDate)(date);
  27. const oldQuarter = Math.trunc(_date.getMonth() / 3) + 1;
  28. const diff = quarter - oldQuarter;
  29. return (0, _index.setMonth)(_date, _date.getMonth() + diff * 3);
  30. }