setHours.mjs 842 B

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