index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = roundToNearestMinutes;
  7. var _index = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index2 = require("../_lib/roundingMethods/index.js");
  9. var _index3 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  10. /**
  11. * @name roundToNearestMinutes
  12. * @category Minute Helpers
  13. * @summary Rounds the given date to the nearest minute
  14. *
  15. * @description
  16. * Rounds the given date to the nearest minute (or number of minutes).
  17. * Rounds up when the given date is exactly between the nearest round minutes.
  18. *
  19. * @param {Date|Number} date - the date to round
  20. * @param {Object} [options] - an object with options.
  21. * @param {Number} [options.nearestTo=1] - nearest number of minutes to round to. E.g. `15` to round to quarter hours.
  22. * @param {String} [options.roundingMethod='trunc'] - a rounding method (`ceil`, `floor`, `round` or `trunc`)
  23. * @returns {Date} the new date rounded to the closest minute
  24. * @throws {TypeError} 1 argument required
  25. * @throws {RangeError} `options.nearestTo` must be between 1 and 30
  26. *
  27. * @example
  28. * // Round 10 July 2014 12:12:34 to nearest minute:
  29. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
  30. * //=> Thu Jul 10 2014 12:13:00
  31. *
  32. * @example
  33. * // Round 10 July 2014 12:07:30 to nearest quarter hour:
  34. * const result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
  35. * // rounds up because given date is exactly between 12:00:00 and 12:15:00
  36. * //=> Thu Jul 10 2014 12:15:00
  37. */
  38. function roundToNearestMinutes(dirtyDate, options) {
  39. var _options$nearestTo;
  40. if (arguments.length < 1) {
  41. throw new TypeError('1 argument required, but only none provided present');
  42. }
  43. var nearestTo = (0, _index3.default)((_options$nearestTo = options === null || options === void 0 ? void 0 : options.nearestTo) !== null && _options$nearestTo !== void 0 ? _options$nearestTo : 1);
  44. if (nearestTo < 1 || nearestTo > 30) {
  45. throw new RangeError('`options.nearestTo` must be between 1 and 30');
  46. }
  47. var date = (0, _index.default)(dirtyDate);
  48. var seconds = date.getSeconds(); // relevant if nearestTo is 1, which is the default case
  49. var minutes = date.getMinutes() + seconds / 60;
  50. var roundingMethod = (0, _index2.getRoundingMethod)(options === null || options === void 0 ? void 0 : options.roundingMethod);
  51. var roundedMinutes = roundingMethod(minutes / nearestTo) * nearestTo;
  52. var remainderMinutes = minutes % nearestTo;
  53. var addedMinutes = Math.round(remainderMinutes / nearestTo) * nearestTo;
  54. return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), roundedMinutes + addedMinutes);
  55. }
  56. module.exports = exports.default;