differenceInCalendarYears.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { normalizeDates } from "./_lib/normalizeDates.js";
  2. /**
  3. * The {@link differenceInCalendarYears} function options.
  4. */
  5. /**
  6. * @name differenceInCalendarYears
  7. * @category Year Helpers
  8. * @summary Get the number of calendar years between the given dates.
  9. *
  10. * @description
  11. * Get the number of calendar years between the given dates.
  12. *
  13. * @param laterDate - The later date
  14. * @param earlierDate - The earlier date
  15. * @param options - An object with options
  16. * @returns The number of calendar years
  17. *
  18. * @example
  19. * // How many calendar years are between 31 December 2013 and 11 February 2015?
  20. * const result = differenceInCalendarYears(
  21. * new Date(2015, 1, 11),
  22. * new Date(2013, 11, 31)
  23. * );
  24. * //=> 2
  25. */
  26. export function differenceInCalendarYears(laterDate, earlierDate, options) {
  27. const [laterDate_, earlierDate_] = normalizeDates(
  28. options?.in,
  29. laterDate,
  30. earlierDate,
  31. );
  32. return laterDate_.getFullYear() - earlierDate_.getFullYear();
  33. }
  34. // Fallback for modularized imports:
  35. export default differenceInCalendarYears;