index.js 1.0 KB

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