-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
index.js
32 lines (29 loc) · 1.08 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
import toDate from '../toDate/index.js'
/**
* @name differenceInCalendarMonths
* @category Month Helpers
* @summary Get the number of calendar months between the given dates.
*
* @description
* Get the number of calendar months 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 months
*
* @example
* // How many calendar months are between 31 January 2014 and 1 September 2014?
* var result = differenceInCalendarMonths(
* new Date(2014, 0, 31),
* new Date(2014, 8, 1)
* )
* //=> 8
*/
export default function differenceInCalendarMonths (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var dateRight = toDate(dirtyDateRight, dirtyOptions)
var yearDiff = dateRight.getFullYear() - dateLeft.getFullYear()
var monthDiff = dateRight.getMonth() - dateLeft.getMonth()
return yearDiff * 12 + monthDiff
}