differenceInSeconds.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { getRoundingMethod } from "./_lib/getRoundingMethod.mjs";
  2. import { differenceInMilliseconds } from "./differenceInMilliseconds.mjs";
  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. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  15. *
  16. * @param dateLeft - The later date
  17. * @param dateRight - The earlier date
  18. * @param options - An object with options.
  19. *
  20. * @returns The number of seconds
  21. *
  22. * @example
  23. * // How many seconds are between
  24. * // 2 July 2014 12:30:07.999 and 2 July 2014 12:30:20.000?
  25. * const result = differenceInSeconds(
  26. * new Date(2014, 6, 2, 12, 30, 20, 0),
  27. * new Date(2014, 6, 2, 12, 30, 7, 999)
  28. * )
  29. * //=> 12
  30. */
  31. export function differenceInSeconds(dateLeft, dateRight, options) {
  32. const diff = differenceInMilliseconds(dateLeft, dateRight) / 1000;
  33. return getRoundingMethod(options?.roundingMethod)(diff);
  34. }
  35. // Fallback for modularized imports:
  36. export default differenceInSeconds;