index.js 858 B

1234567891011121314151617181920212223242526272829
  1. var parse = require('../parse/index.js')
  2. /**
  3. * @category Year Helpers
  4. * @summary Get the number of calendar years between the given dates.
  5. *
  6. * @description
  7. * Get the number of calendar years between the given dates.
  8. *
  9. * @param {Date|String|Number} dateLeft - the later date
  10. * @param {Date|String|Number} dateRight - the earlier date
  11. * @returns {Number} the number of calendar years
  12. *
  13. * @example
  14. * // How many calendar years are between 31 December 2013 and 11 February 2015?
  15. * var result = differenceInCalendarYears(
  16. * new Date(2015, 1, 11),
  17. * new Date(2013, 11, 31)
  18. * )
  19. * //=> 2
  20. */
  21. function differenceInCalendarYears (dirtyDateLeft, dirtyDateRight) {
  22. var dateLeft = parse(dirtyDateLeft)
  23. var dateRight = parse(dirtyDateRight)
  24. return dateLeft.getFullYear() - dateRight.getFullYear()
  25. }
  26. module.exports = differenceInCalendarYears