index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = roundToNearestMinutes;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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. * ### v2.0.0 breaking changes:
  19. *
  20. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  21. *
  22. * @param {Date|Number} date - the date to round
  23. * @param {Object} [options] - an object with options.
  24. * @param {Number} [options.nearestTo=1] - nearest number of minutes to round to. E.g. `15` to round to quarter hours.
  25. * @returns {Date} the new date rounded to the closest minute
  26. * @throws {TypeError} 1 argument required
  27. * @throws {RangeError} `options.nearestTo` must be between 1 and 30
  28. *
  29. * @example
  30. * // Round 10 July 2014 12:12:34 to nearest minute:
  31. * var result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34))
  32. * //=> Thu Jul 10 2014 12:13:00
  33. *
  34. * @example
  35. * // Round 10 July 2014 12:07:30 to nearest quarter hour:
  36. * var result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34), { nearestTo: 15 })
  37. * // rounds up because given date is exactly between 12:00:00 and 12:15:00
  38. * //=> Thu Jul 10 2014 12:15:00
  39. */
  40. function roundToNearestMinutes(dirtyDate, options) {
  41. if (arguments.length < 1) {
  42. throw new TypeError('1 argument required, but only none provided present');
  43. }
  44. var nearestTo = options && 'nearestTo' in options ? (0, _index2.default)(options.nearestTo) : 1;
  45. if (nearestTo < 1 || nearestTo > 30) {
  46. throw new RangeError('`options.nearestTo` must be between 1 and 30');
  47. }
  48. var date = (0, _index.default)(dirtyDate);
  49. var seconds = date.getSeconds(); // relevant if nearestTo is 1, which is the default case
  50. var minutes = date.getMinutes() + seconds / 60;
  51. var roundedMinutes = Math.floor(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;