index.js 931 B

1234567891011121314151617181920212223242526272829
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name startOfQuarter
  5. * @category Quarter Helpers
  6. * @summary Return the start of a year quarter for the given date.
  7. *
  8. * @description
  9. * Return the start 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. * @returns {Date} the start of a quarter
  14. * @throws {TypeError} 1 argument required
  15. *
  16. * @example
  17. * // The start of a quarter for 2 September 2014 11:55:00:
  18. * const result = startOfQuarter(new Date(2014, 8, 2, 11, 55, 0))
  19. * //=> Tue Jul 01 2014 00:00:00
  20. */
  21. export default function startOfQuarter(dirtyDate) {
  22. requiredArgs(1, arguments);
  23. var date = toDate(dirtyDate);
  24. var currentMonth = date.getMonth();
  25. var month = currentMonth - currentMonth % 3;
  26. date.setMonth(month, 1);
  27. date.setHours(0, 0, 0, 0);
  28. return date;
  29. }