index.js 985 B

1234567891011121314151617181920212223242526272829
  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. * @param {Date|Number} dateLeft - the later date
  12. * @param {Date|Number} dateRight - the earlier date
  13. * @returns {Number} the number of calendar years
  14. * @throws {TypeError} 2 arguments required
  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 default function differenceInCalendarYears(dirtyDateLeft, dirtyDateRight) {
  25. requiredArgs(2, arguments);
  26. var dateLeft = toDate(dirtyDateLeft);
  27. var dateRight = toDate(dirtyDateRight);
  28. return dateLeft.getFullYear() - dateRight.getFullYear();
  29. }