index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { millisecondsInHour } from "../constants/index.js";
  2. import differenceInMilliseconds from "../differenceInMilliseconds/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  4. import { getRoundingMethod } from "../_lib/roundingMethods/index.js";
  5. /**
  6. * @name differenceInHours
  7. * @category Hour Helpers
  8. * @summary Get the number of hours between the given dates.
  9. *
  10. * @description
  11. * Get the number of hours between the given dates.
  12. *
  13. * @param {Date|Number} dateLeft - the later date
  14. * @param {Date|Number} dateRight - the earlier date
  15. * @param {Object} [options] - an object with options.
  16. * @param {String} [options.roundingMethod='trunc'] - a rounding method (`ceil`, `floor`, `round` or `trunc`)
  17. * @returns {Number} the number of hours
  18. * @throws {TypeError} 2 arguments required
  19. *
  20. * @example
  21. * // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
  22. * const result = differenceInHours(
  23. * new Date(2014, 6, 2, 19, 0),
  24. * new Date(2014, 6, 2, 6, 50)
  25. * )
  26. * //=> 12
  27. */
  28. export default function differenceInHours(dateLeft, dateRight, options) {
  29. requiredArgs(2, arguments);
  30. var diff = differenceInMilliseconds(dateLeft, dateRight) / millisecondsInHour;
  31. return getRoundingMethod(options === null || options === void 0 ? void 0 : options.roundingMethod)(diff);
  32. }