index.js 1.1 KB

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