setDate.mjs 897 B

123456789101112131415161718192021222324252627282930
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name setDate
  4. * @category Day Helpers
  5. * @summary Set the day of the month to the given date.
  6. *
  7. * @description
  8. * Set the day of the month to the given date.
  9. *
  10. * @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).
  11. *
  12. * @param date - The date to be changed
  13. * @param dayOfMonth - The day of the month of the new date
  14. *
  15. * @returns The new date with the day of the month set
  16. *
  17. * @example
  18. * // Set the 30th day of the month to 1 September 2014:
  19. * const result = setDate(new Date(2014, 8, 1), 30)
  20. * //=> Tue Sep 30 2014 00:00:00
  21. */
  22. export function setDate(date, dayOfMonth) {
  23. const _date = toDate(date);
  24. _date.setDate(dayOfMonth);
  25. return _date;
  26. }
  27. // Fallback for modularized imports:
  28. export default setDate;