index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name lastDayOfQuarter
  5. * @category Quarter Helpers
  6. * @summary Return the last day of a year quarter for the given date.
  7. *
  8. * @description
  9. * Return the last day of a year quarter for the given date.
  10. * The result will be in the local timezone.
  11. *
  12. * ### v2.0.0 breaking changes:
  13. *
  14. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  15. *
  16. * @param {Date|Number} date - the original date
  17. * @param {Object} [options] - an object with options.
  18. * @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
  19. * @returns {Date} the last day of a quarter
  20. * @throws {TypeError} 1 argument required
  21. * @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
  22. *
  23. * @example
  24. * // The last day of a quarter for 2 September 2014 11:55:00:
  25. * var result = lastDayOfQuarter(new Date(2014, 8, 2, 11, 55, 0))
  26. * //=> Tue Sep 30 2014 00:00:00
  27. */
  28. export default function lastDayOfQuarter(dirtyDate) {
  29. requiredArgs(1, arguments);
  30. var date = toDate(dirtyDate);
  31. var currentMonth = date.getMonth();
  32. var month = currentMonth - currentMonth % 3 + 3;
  33. date.setMonth(month, 0);
  34. date.setHours(0, 0, 0, 0);
  35. return date;
  36. }