index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import differenceInMilliseconds from "../differenceInMilliseconds/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. import { getRoundingMethod } from "../_lib/roundingMethods/index.js";
  4. /**
  5. * @name differenceInSeconds
  6. * @category Second Helpers
  7. * @summary Get the number of seconds between the given dates.
  8. *
  9. * @description
  10. * Get the number of seconds between the given dates.
  11. *
  12. * @param {Date|Number} dateLeft - the later date
  13. * @param {Date|Number} dateRight - the earlier date
  14. * @param {Object} [options] - an object with options.
  15. * @param {String} [options.roundingMethod='trunc'] - a rounding method (`ceil`, `floor`, `round` or `trunc`)
  16. * @returns {Number} the number of seconds
  17. * @throws {TypeError} 2 arguments required
  18. *
  19. * @example
  20. * // How many seconds are between
  21. * // 2 July 2014 12:30:07.999 and 2 July 2014 12:30:20.000?
  22. * const result = differenceInSeconds(
  23. * new Date(2014, 6, 2, 12, 30, 20, 0),
  24. * new Date(2014, 6, 2, 12, 30, 7, 999)
  25. * )
  26. * //=> 12
  27. */
  28. export default function differenceInSeconds(dateLeft, dateRight, options) {
  29. requiredArgs(2, arguments);
  30. var diff = differenceInMilliseconds(dateLeft, dateRight) / 1000;
  31. return getRoundingMethod(options === null || options === void 0 ? void 0 : options.roundingMethod)(diff);
  32. }