index.js 1.0 KB

12345678910111213141516171819202122232425262728
  1. import toInteger from "../_lib/toInteger/index.js";
  2. import addMilliseconds from "../addMilliseconds/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  4. var MILLISECONDS_IN_HOUR = 3600000;
  5. /**
  6. * @name addHours
  7. * @category Hour Helpers
  8. * @summary Add the specified number of hours to the given date.
  9. *
  10. * @description
  11. * Add the specified number of hours to the given date.
  12. *
  13. * @param {Date|Number} date - the date to be changed
  14. * @param {Number} amount - the amount of hours to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
  15. * @returns {Date} the new date with the hours added
  16. * @throws {TypeError} 2 arguments required
  17. *
  18. * @example
  19. * // Add 2 hours to 10 July 2014 23:00:00:
  20. * const result = addHours(new Date(2014, 6, 10, 23, 0), 2)
  21. * //=> Fri Jul 11 2014 01:00:00
  22. */
  23. export default function addHours(dirtyDate, dirtyAmount) {
  24. requiredArgs(2, arguments);
  25. var amount = toInteger(dirtyAmount);
  26. return addMilliseconds(dirtyDate, amount * MILLISECONDS_IN_HOUR);
  27. }