index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. import differenceInMilliseconds from "../differenceInMilliseconds/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. var MILLISECONDS_IN_HOUR = 3600000;
  4. /**
  5. * @name differenceInHours
  6. * @category Hour Helpers
  7. * @summary Get the number of hours between the given dates.
  8. *
  9. * @description
  10. * Get the number of hours between the given dates.
  11. *
  12. * ### v2.0.0 breaking changes:
  13. *
  14. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  15. *
  16. * @param {Date|Number} dateLeft - the later date
  17. * @param {Date|Number} dateRight - the earlier date
  18. * @returns {Number} the number of hours
  19. * @throws {TypeError} 2 arguments required
  20. *
  21. * @example
  22. * // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
  23. * var result = differenceInHours(
  24. * new Date(2014, 6, 2, 19, 0),
  25. * new Date(2014, 6, 2, 6, 50)
  26. * )
  27. * //=> 12
  28. */
  29. export default function differenceInHours(dirtyDateLeft, dirtyDateRight) {
  30. requiredArgs(2, arguments);
  31. var diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / MILLISECONDS_IN_HOUR;
  32. return diff > 0 ? Math.floor(diff) : Math.ceil(diff);
  33. }