differenceInCalendarYears.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name differenceInCalendarYears
  4. * @category Year Helpers
  5. * @summary Get the number of calendar years between the given dates.
  6. *
  7. * @description
  8. * Get the number of calendar years between the given dates.
  9. *
  10. * @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).
  11. *
  12. * @param dateLeft - The later date
  13. * @param dateRight - The earlier date
  14. * @returns The number of calendar years
  15. *
  16. * @example
  17. * // How many calendar years are between 31 December 2013 and 11 February 2015?
  18. * const result = differenceInCalendarYears(
  19. * new Date(2015, 1, 11),
  20. * new Date(2013, 11, 31)
  21. * )
  22. * //=> 2
  23. */
  24. export function differenceInCalendarYears(dateLeft, dateRight) {
  25. const _dateLeft = toDate(dateLeft);
  26. const _dateRight = toDate(dateRight);
  27. return _dateLeft.getFullYear() - _dateRight.getFullYear();
  28. }
  29. // Fallback for modularized imports:
  30. export default differenceInCalendarYears;