index.js 1.2 KB

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