endOfQuarter.cjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. exports.endOfQuarter = endOfQuarter;
  3. var _index = require("./toDate.cjs");
  4. /**
  5. * The {@link endOfQuarter} function options.
  6. */
  7. /**
  8. * @name endOfQuarter
  9. * @category Quarter Helpers
  10. * @summary Return the end of a year quarter for the given date.
  11. *
  12. * @description
  13. * Return the end of a year quarter for the given date.
  14. * The result will be in the local timezone.
  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 original date
  20. * @param options - An object with options
  21. *
  22. * @returns The end of a quarter
  23. *
  24. * @example
  25. * // The end of a quarter for 2 September 2014 11:55:00:
  26. * const result = endOfQuarter(new Date(2014, 8, 2, 11, 55, 0))
  27. * //=> Tue Sep 30 2014 23:59:59.999
  28. */
  29. function endOfQuarter(date, options) {
  30. const _date = (0, _index.toDate)(date, options?.in);
  31. const currentMonth = _date.getMonth();
  32. const month = currentMonth - (currentMonth % 3) + 3;
  33. _date.setMonth(month, 0);
  34. _date.setHours(23, 59, 59, 999);
  35. return _date;
  36. }