roundToNearestHours.cjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. exports.roundToNearestHours = roundToNearestHours;
  3. var _index = require("./_lib/getRoundingMethod.cjs");
  4. var _index2 = require("./constructFrom.cjs");
  5. var _index3 = require("./toDate.cjs");
  6. /**
  7. * The {@link roundToNearestHours} function options.
  8. */
  9. /**
  10. * @name roundToNearestHours
  11. * @category Hour Helpers
  12. * @summary Rounds the given date to the nearest hour
  13. *
  14. * @description
  15. * Rounds the given date to the nearest hour (or number of hours).
  16. * Rounds up when the given date is exactly between the nearest round hours.
  17. *
  18. * @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).
  19. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  20. *
  21. * @param date - The date to round
  22. * @param options - An object with options.
  23. *
  24. * @returns The new date rounded to the closest hour
  25. *
  26. * @example
  27. * // Round 10 July 2014 12:34:56 to nearest hour:
  28. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56))
  29. * //=> Thu Jul 10 2014 13:00:00
  30. *
  31. * @example
  32. * // Round 10 July 2014 12:34:56 to nearest half hour:
  33. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 6 })
  34. * //=> Thu Jul 10 2014 12:00:00
  35. *
  36. * @example
  37. * // Round 10 July 2014 12:34:56 to nearest half hour:
  38. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { nearestTo: 8 })
  39. * //=> Thu Jul 10 2014 16:00:00
  40. *
  41. * @example
  42. * // Floor (rounds down) 10 July 2014 12:34:56 to nearest hour:
  43. * const result = roundToNearestHours(new Date(2014, 6, 10, 1, 23, 45), { roundingMethod: 'ceil' })
  44. * //=> Thu Jul 10 2014 02:00:00
  45. *
  46. * @example
  47. * // Ceil (rounds up) 10 July 2014 12:34:56 to nearest quarter hour:
  48. * const result = roundToNearestHours(new Date(2014, 6, 10, 12, 34, 56), { roundingMethod: 'floor', nearestTo: 8 })
  49. * //=> Thu Jul 10 2014 08:00:00
  50. */
  51. function roundToNearestHours(date, options) {
  52. const nearestTo = options?.nearestTo ?? 1;
  53. if (nearestTo < 1 || nearestTo > 12)
  54. return (0, _index2.constructFrom)(options?.in || date, NaN);
  55. const date_ = (0, _index3.toDate)(date, options?.in);
  56. const fractionalMinutes = date_.getMinutes() / 60;
  57. const fractionalSeconds = date_.getSeconds() / 60 / 60;
  58. const fractionalMilliseconds = date_.getMilliseconds() / 1000 / 60 / 60;
  59. const hours =
  60. date_.getHours() +
  61. fractionalMinutes +
  62. fractionalSeconds +
  63. fractionalMilliseconds;
  64. const method = options?.roundingMethod ?? "round";
  65. const roundingMethod = (0, _index.getRoundingMethod)(method);
  66. const roundedHours = roundingMethod(hours / nearestTo) * nearestTo;
  67. date_.setHours(roundedHours, 0, 0, 0);
  68. return date_;
  69. }