index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name differenceInCalendarYears
  5. * @category Year Helpers
  6. * @summary Get the number of calendar years between the given dates.
  7. *
  8. * @description
  9. * Get the number of calendar years between the given dates.
  10. *
  11. * ### v2.0.0 breaking changes:
  12. *
  13. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  14. *
  15. * @param {Date|Number} dateLeft - the later date
  16. * @param {Date|Number} dateRight - the earlier date
  17. * @returns {Number} the number of calendar years
  18. * @throws {TypeError} 2 arguments required
  19. *
  20. * @example
  21. * // How many calendar years are between 31 December 2013 and 11 February 2015?
  22. * const result = differenceInCalendarYears(
  23. * new Date(2015, 1, 11),
  24. * new Date(2013, 11, 31)
  25. * )
  26. * //=> 2
  27. */
  28. export default function differenceInCalendarYears(dirtyDateLeft, dirtyDateRight) {
  29. requiredArgs(2, arguments);
  30. var dateLeft = toDate(dirtyDateLeft);
  31. var dateRight = toDate(dirtyDateRight);
  32. return dateLeft.getFullYear() - dateRight.getFullYear();
  33. }