lastDayOfQuarter.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link lastDayOfQuarter} function options.
  4. */
  5. /**
  6. * @name lastDayOfQuarter
  7. * @category Quarter Helpers
  8. * @summary Return the last day of a year quarter for the given date.
  9. *
  10. * @description
  11. * Return the last day of a year quarter for the given date.
  12. * The result will be in the local timezone.
  13. *
  14. * @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).
  15. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  16. *
  17. * @param date - The original date
  18. * @param options - The options
  19. *
  20. * @returns The last day of a quarter
  21. *
  22. * @example
  23. * // The last day of a quarter for 2 September 2014 11:55:00:
  24. * const result = lastDayOfQuarter(new Date(2014, 8, 2, 11, 55, 0))
  25. * //=> Tue Sep 30 2014 00:00:00
  26. */
  27. export function lastDayOfQuarter(date, options) {
  28. const date_ = toDate(date, options?.in);
  29. const currentMonth = date_.getMonth();
  30. const month = currentMonth - (currentMonth % 3) + 3;
  31. date_.setMonth(month, 0);
  32. date_.setHours(0, 0, 0, 0);
  33. return date_;
  34. }
  35. // Fallback for modularized imports:
  36. export default lastDayOfQuarter;