index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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. * @param {Date|Number} date - the original date
  13. * @param {Object} [options] - an object with options.
  14. * @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
  15. * @returns {Date} the last day of a quarter
  16. * @throws {TypeError} 1 argument required
  17. * @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2
  18. *
  19. * @example
  20. * // The last day of a quarter for 2 September 2014 11:55:00:
  21. * const result = lastDayOfQuarter(new Date(2014, 8, 2, 11, 55, 0))
  22. * //=> Tue Sep 30 2014 00:00:00
  23. */
  24. export default function lastDayOfQuarter(dirtyDate) {
  25. requiredArgs(1, arguments);
  26. var date = toDate(dirtyDate);
  27. var currentMonth = date.getMonth();
  28. var month = currentMonth - currentMonth % 3 + 3;
  29. date.setMonth(month, 0);
  30. date.setHours(0, 0, 0, 0);
  31. return date;
  32. }