-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index.js
33 lines (30 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import getQuarter from '../getQuarter/index.js'
import toDate from '../toDate/index.js'
/**
* @name differenceInCalendarQuarters
* @category Quarter Helpers
* @summary Get the number of calendar quarters between the given dates.
*
* @description
* Get the number of calendar quarters between the given dates.
*
* @param {Date|String|Number} dateLeft - the earlier date
* @param {Date|String|Number} dateRight - the later date
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @returns {Number} the number of calendar quarters
*
* @example
* // How many calendar quarters are between 31 December 2013 and 2 July 2014?
* var result = differenceInCalendarQuarters(
* new Date(2013, 11, 31),
* new Date(2014, 6, 2)
* )
* //=> 3
*/
export default function differenceInCalendarQuarters (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var dateRight = toDate(dirtyDateRight, dirtyOptions)
var yearDiff = dateRight.getFullYear() - dateLeft.getFullYear()
var quarterDiff = getQuarter(dateRight, dirtyOptions) - getQuarter(dateLeft, dirtyOptions)
return yearDiff * 4 + quarterDiff
}