differenceInCalendarYears.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. exports.differenceInCalendarYears = differenceInCalendarYears;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name differenceInCalendarYears
  6. * @category Year Helpers
  7. * @summary Get the number of calendar years between the given dates.
  8. *
  9. * @description
  10. * Get the number of calendar years between the given dates.
  11. *
  12. * @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).
  13. *
  14. * @param dateLeft - The later date
  15. * @param dateRight - The earlier date
  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. function differenceInCalendarYears(dateLeft, dateRight) {
  27. const _dateLeft = (0, _index.toDate)(dateLeft);
  28. const _dateRight = (0, _index.toDate)(dateRight);
  29. return _dateLeft.getFullYear() - _dateRight.getFullYear();
  30. }