startOfMonth.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link startOfMonth} function options.
  4. */
  5. /**
  6. * @name startOfMonth
  7. * @category Month Helpers
  8. * @summary Return the start of a month for the given date.
  9. *
  10. * @description
  11. * Return the start of a month for the given date. The result will be in the local timezone.
  12. *
  13. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments.
  14. * 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,
  16. * or inferred from the arguments.
  17. *
  18. * @param date - The original date
  19. * @param options - An object with options
  20. *
  21. * @returns The start of a month
  22. *
  23. * @example
  24. * // The start of a month for 2 September 2014 11:55:00:
  25. * const result = startOfMonth(new Date(2014, 8, 2, 11, 55, 0))
  26. * //=> Mon Sep 01 2014 00:00:00
  27. */
  28. export function startOfMonth(date, options) {
  29. const _date = toDate(date, options?.in);
  30. _date.setDate(1);
  31. _date.setHours(0, 0, 0, 0);
  32. return _date;
  33. }
  34. // Fallback for modularized imports:
  35. export default startOfMonth;