differenceInSeconds.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { getRoundingMethod } from "./_lib/getRoundingMethod.js";
  2. import { differenceInMilliseconds } from "./differenceInMilliseconds.js";
  3. /**
  4. * The {@link differenceInSeconds} function options.
  5. */
  6. /**
  7. * @name differenceInSeconds
  8. * @category Second Helpers
  9. * @summary Get the number of seconds between the given dates.
  10. *
  11. * @description
  12. * Get the number of seconds between the given dates.
  13. *
  14. * @param laterDate - The later date
  15. * @param earlierDate - The earlier date
  16. * @param options - An object with options.
  17. *
  18. * @returns The number of seconds
  19. *
  20. * @example
  21. * // How many seconds are between
  22. * // 2 July 2014 12:30:07.999 and 2 July 2014 12:30:20.000?
  23. * const result = differenceInSeconds(
  24. * new Date(2014, 6, 2, 12, 30, 20, 0),
  25. * new Date(2014, 6, 2, 12, 30, 7, 999)
  26. * )
  27. * //=> 12
  28. */
  29. export function differenceInSeconds(laterDate, earlierDate, options) {
  30. const diff = differenceInMilliseconds(laterDate, earlierDate) / 1000;
  31. return getRoundingMethod(options?.roundingMethod)(diff);
  32. }
  33. // Fallback for modularized imports:
  34. export default differenceInSeconds;