roundToNearestMinutes.cjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. exports.roundToNearestMinutes = roundToNearestMinutes;
  3. var _index = require("./_lib/getRoundingMethod.cjs");
  4. var _index2 = require("./constructFrom.cjs");
  5. var _index3 = require("./toDate.cjs");
  6. /**
  7. * The {@link roundToNearestMinutes} function options.
  8. */
  9. /**
  10. * @name roundToNearestMinutes
  11. * @category Minute Helpers
  12. * @summary Rounds the given date to the nearest minute
  13. *
  14. * @description
  15. * Rounds the given date to the nearest minute (or number of minutes).
  16. * Rounds up when the given date is exactly between the nearest round minutes.
  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 minute
  25. *
  26. * @example
  27. * // Round 10 July 2014 12:12:34 to nearest minute:
  28. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
  29. * //=> Thu Jul 10 2014 12:13:00
  30. *
  31. * @example
  32. * // Round 10 July 2014 12:12:34 to nearest quarter hour:
  33. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
  34. * //=> Thu Jul 10 2014 12:15:00
  35. *
  36. * @example
  37. * // Floor (rounds down) 10 July 2014 12:12:34 to nearest minute:
  38. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'floor' })
  39. * //=> Thu Jul 10 2014 12:12:00
  40. *
  41. * @example
  42. * // Ceil (rounds up) 10 July 2014 12:12:34 to nearest half hour:
  43. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'ceil', nearestTo: 30 })
  44. * //=> Thu Jul 10 2014 12:30:00
  45. */
  46. function roundToNearestMinutes(date, options) {
  47. const nearestTo = options?.nearestTo ?? 1;
  48. if (nearestTo < 1 || nearestTo > 30)
  49. return (0, _index2.constructFrom)(date, NaN);
  50. const date_ = (0, _index3.toDate)(date, options?.in);
  51. const fractionalSeconds = date_.getSeconds() / 60;
  52. const fractionalMilliseconds = date_.getMilliseconds() / 1000 / 60;
  53. const minutes =
  54. date_.getMinutes() + fractionalSeconds + fractionalMilliseconds;
  55. const method = options?.roundingMethod ?? "round";
  56. const roundingMethod = (0, _index.getRoundingMethod)(method);
  57. const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo;
  58. date_.setMinutes(roundedMinutes, 0, 0);
  59. return date_;
  60. }