roundToNearestMinutes.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. exports.roundToNearestMinutes = roundToNearestMinutes;
  3. var _index = require("./_lib/getRoundingMethod.js");
  4. var _index2 = require("./constructFrom.js");
  5. var _index3 = require("./toDate.js");
  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. *
  20. * @param date - The date to round
  21. * @param options - An object with options.
  22. *
  23. * @returns The new date rounded to the closest minute
  24. *
  25. * @example
  26. * // Round 10 July 2014 12:12:34 to nearest minute:
  27. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
  28. * //=> Thu Jul 10 2014 12:13:00
  29. *
  30. * @example
  31. * // Round 10 July 2014 12:12:34 to nearest quarter hour:
  32. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
  33. * //=> Thu Jul 10 2014 12:15:00
  34. *
  35. * @example
  36. * // Floor (rounds down) 10 July 2014 12:12:34 to nearest minute:
  37. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'floor' })
  38. * //=> Thu Jul 10 2014 12:12:00
  39. *
  40. * @example
  41. * // Ceil (rounds up) 10 July 2014 12:12:34 to nearest half hour:
  42. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { roundingMethod: 'ceil', nearestTo: 30 })
  43. * //=> Thu Jul 10 2014 12:30:00
  44. */
  45. function roundToNearestMinutes(date, options) {
  46. const nearestTo = options?.nearestTo ?? 1;
  47. if (nearestTo < 1 || nearestTo > 30)
  48. return (0, _index2.constructFrom)(date, NaN);
  49. const _date = (0, _index3.toDate)(date);
  50. const fractionalSeconds = _date.getSeconds() / 60;
  51. const fractionalMilliseconds = _date.getMilliseconds() / 1000 / 60;
  52. const minutes =
  53. _date.getMinutes() + fractionalSeconds + fractionalMilliseconds;
  54. // Unlike the `differenceIn*` functions, the default rounding behavior is `round` and not 'trunc'
  55. const method = options?.roundingMethod ?? "round";
  56. const roundingMethod = (0, _index.getRoundingMethod)(method);
  57. const roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo;
  58. const result = (0, _index2.constructFrom)(date, _date);
  59. result.setMinutes(roundedMinutes, 0, 0);
  60. return result;
  61. }