differenceInMinutes.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { getRoundingMethod } from "./_lib/getRoundingMethod.mjs";
  2. import { millisecondsInMinute } from "./constants.mjs";
  3. import { differenceInMilliseconds } from "./differenceInMilliseconds.mjs";
  4. /**
  5. * The {@link differenceInMinutes} function options.
  6. */
  7. /**
  8. * @name differenceInMinutes
  9. * @category Minute Helpers
  10. * @summary Get the number of minutes between the given dates.
  11. *
  12. * @description
  13. * Get the signed number of full (rounded towards 0) minutes between the given dates.
  14. *
  15. * @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).
  16. *
  17. * @param dateLeft - The later date
  18. * @param dateRight - The earlier date
  19. * @param options - An object with options.
  20. *
  21. * @returns The number of minutes
  22. *
  23. * @example
  24. * // How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
  25. * const result = differenceInMinutes(
  26. * new Date(2014, 6, 2, 12, 20, 0),
  27. * new Date(2014, 6, 2, 12, 7, 59)
  28. * )
  29. * //=> 12
  30. *
  31. * @example
  32. * // How many minutes are between 10:01:59 and 10:00:00
  33. * const result = differenceInMinutes(
  34. * new Date(2000, 0, 1, 10, 0, 0),
  35. * new Date(2000, 0, 1, 10, 1, 59)
  36. * )
  37. * //=> -1
  38. */
  39. export function differenceInMinutes(dateLeft, dateRight, options) {
  40. const diff =
  41. differenceInMilliseconds(dateLeft, dateRight) / millisecondsInMinute;
  42. return getRoundingMethod(options?.roundingMethod)(diff);
  43. }
  44. // Fallback for modularized imports:
  45. export default differenceInMinutes;