index.js 904 B

1234567891011121314151617181920212223242526272829
  1. var differenceInMilliseconds = require('../difference_in_milliseconds/index.js')
  2. var MILLISECONDS_IN_HOUR = 3600000
  3. /**
  4. * @category Hour Helpers
  5. * @summary Get the number of hours between the given dates.
  6. *
  7. * @description
  8. * Get the number of hours between the given dates.
  9. *
  10. * @param {Date|String|Number} dateLeft - the later date
  11. * @param {Date|String|Number} dateRight - the earlier date
  12. * @returns {Number} the number of hours
  13. *
  14. * @example
  15. * // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
  16. * var result = differenceInHours(
  17. * new Date(2014, 6, 2, 19, 0),
  18. * new Date(2014, 6, 2, 6, 50)
  19. * )
  20. * //=> 12
  21. */
  22. function differenceInHours (dirtyDateLeft, dirtyDateRight) {
  23. var diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / MILLISECONDS_IN_HOUR
  24. return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
  25. }
  26. module.exports = differenceInHours