startOfHour.mjs 873 B

123456789101112131415161718192021222324252627282930
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name startOfHour
  4. * @category Hour Helpers
  5. * @summary Return the start of an hour for the given date.
  6. *
  7. * @description
  8. * Return the start of an hour 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 an hour
  16. *
  17. * @example
  18. * // The start of an hour for 2 September 2014 11:55:00:
  19. * const result = startOfHour(new Date(2014, 8, 2, 11, 55))
  20. * //=> Tue Sep 02 2014 11:00:00
  21. */
  22. export function startOfHour(date) {
  23. const _date = toDate(date);
  24. _date.setMinutes(0, 0, 0);
  25. return _date;
  26. }
  27. // Fallback for modularized imports:
  28. export default startOfHour;