roundToNearestHours.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { getRoundingMethod } from "./_lib/getRoundingMethod.mjs";
  2. import { constructFrom } from "./constructFrom.mjs";
  3. import { toDate } from "./toDate.mjs";
  4. /**
  5. * The {@link roundToNearestHours} function options.
  6. */
  7. /**
  8. * @name roundToNearestHours
  9. * @category Hour Helpers
  10. * @summary Rounds the given date to the nearest hour
  11. *
  12. * @description
  13. * Rounds the given date to the nearest hour (or number of hours).
  14. * Rounds up when the given date is exactly between the nearest round hours.
  15. *
  16. * @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).
  17. *
  18. * @param date - The date to round
  19. * @param options - An object with options.
  20. *
  21. * @returns The new date rounded to the closest hour
  22. *
  23. * @example
  24. * // Round 10 July 2014 12:34:56 to nearest hour:
  25. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56))
  26. * //=> Thu Jul 10 2014 13:00:00
  27. *
  28. * @example
  29. * // Round 10 July 2014 12:34:56 to nearest half hour:
  30. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 6 })
  31. * //=> Thu Jul 10 2014 12:00:00
  32. * @example
  33. * // Round 10 July 2014 12:34:56 to nearest half hour:
  34. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 8 })
  35. * //=> Thu Jul 10 2014 16:00:00
  36. * @example
  37. * // Floor (rounds down) 10 July 2014 12:34:56 to nearest hour:
  38. * const result = roundToNearestHours(new Date(2014, 6, 10, 1, 23, 45), { roundingMethod: 'ceil' })
  39. * //=> Thu Jul 10 2014 02:00:00
  40. *
  41. * @example
  42. * // Ceil (rounds up) 10 July 2014 12:34:56 to nearest quarter hour:
  43. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { roundingMethod: 'floor', nearestTo: 8 })
  44. * //=> Thu Jul 10 2014 08:00:00
  45. */
  46. export function roundToNearestHours(date, options) {
  47. const nearestTo = options?.nearestTo ?? 1;
  48. if (nearestTo < 1 || nearestTo > 12) return constructFrom(date, NaN);
  49. const _date = toDate(date);
  50. const fractionalMinutes = _date.getMinutes() / 60;
  51. const fractionalSeconds = _date.getSeconds() / 60 / 60;
  52. const fractionalMilliseconds = _date.getMilliseconds() / 1000 / 60 / 60;
  53. const hours =
  54. _date.getHours() +
  55. fractionalMinutes +
  56. fractionalSeconds +
  57. fractionalMilliseconds;
  58. // Unlike the `differenceIn*` functions, the default rounding behavior is `round` and not 'trunc'
  59. const method = options?.roundingMethod ?? "round";
  60. const roundingMethod = getRoundingMethod(method);
  61. // nearestTo option does not care daylight savings time
  62. const roundedHours = roundingMethod(hours / nearestTo) * nearestTo;
  63. const result = constructFrom(date, _date);
  64. result.setHours(roundedHours, 0, 0, 0);
  65. return result;
  66. }
  67. // Fallback for modularized imports:
  68. export default roundToNearestHours;