setQuarter.cjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. exports.setQuarter = setQuarter;
  3. var _index = require("./setMonth.cjs");
  4. var _index2 = require("./toDate.cjs");
  5. /**
  6. * The {@link setQuarter} function options.
  7. */
  8. /**
  9. * @name setQuarter
  10. * @category Quarter Helpers
  11. * @summary Set the year quarter to the given date.
  12. *
  13. * @description
  14. * Set the year quarter to the given date.
  15. *
  16. * @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).
  17. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  18. *
  19. * @param date - The date to be changed
  20. * @param quarter - The quarter of the new date
  21. * @param options - The options
  22. *
  23. * @returns The new date with the quarter set
  24. *
  25. * @example
  26. * // Set the 2nd quarter to 2 July 2014:
  27. * const result = setQuarter(new Date(2014, 6, 2), 2)
  28. * //=> Wed Apr 02 2014 00:00:00
  29. */
  30. function setQuarter(date, quarter, options) {
  31. const date_ = (0, _index2.toDate)(date, options?.in);
  32. const oldQuarter = Math.trunc(date_.getMonth() / 3) + 1;
  33. const diff = quarter - oldQuarter;
  34. return (0, _index.setMonth)(date_, date_.getMonth() + diff * 3);
  35. }