index.js 968 B

1234567891011121314151617181920212223242526272829303132
  1. var parse = require('../parse/index.js')
  2. /**
  3. * @category Month Helpers
  4. * @summary Get the number of calendar months between the given dates.
  5. *
  6. * @description
  7. * Get the number of calendar months 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 months
  12. *
  13. * @example
  14. * // How many calendar months are between 31 January 2014 and 1 September 2014?
  15. * var result = differenceInCalendarMonths(
  16. * new Date(2014, 8, 1),
  17. * new Date(2014, 0, 31)
  18. * )
  19. * //=> 8
  20. */
  21. function differenceInCalendarMonths (dirtyDateLeft, dirtyDateRight) {
  22. var dateLeft = parse(dirtyDateLeft)
  23. var dateRight = parse(dirtyDateRight)
  24. var yearDiff = dateLeft.getFullYear() - dateRight.getFullYear()
  25. var monthDiff = dateLeft.getMonth() - dateRight.getMonth()
  26. return yearDiff * 12 + monthDiff
  27. }
  28. module.exports = differenceInCalendarMonths