startOfMonth.mjs 902 B

12345678910111213141516171819202122232425262728293031
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name startOfMonth
  4. * @category Month Helpers
  5. * @summary Return the start of a month for the given date.
  6. *
  7. * @description
  8. * Return the start of a month for the given date.
  9. * The result will be in the local timezone.
  10. *
  11. * @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).
  12. *
  13. * @param date - The original date
  14. *
  15. * @returns The start of a month
  16. *
  17. * @example
  18. * // The start of a month for 2 September 2014 11:55:00:
  19. * const result = startOfMonth(new Date(2014, 8, 2, 11, 55, 0))
  20. * //=> Mon Sep 01 2014 00:00:00
  21. */
  22. export function startOfMonth(date) {
  23. const _date = toDate(date);
  24. _date.setDate(1);
  25. _date.setHours(0, 0, 0, 0);
  26. return _date;
  27. }
  28. // Fallback for modularized imports:
  29. export default startOfMonth;