import toDate from '../toDate/index' ; import differenceInCalendarMonths from '../differenceInCalendarMonths/index' ; import compareAsc from '../compareAsc/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; import isLastDayOfMonth from '../isLastDayOfMonth/index' ; name category summary description param { Date | Number } dateLeft param { Date | Number } dateRight returns { Number } throws example export default function differenceInMonths ( dirtyDateLeft , dirtyDateRight ) { requiredArgs ( 2 , arguments ) ; const dateLeft = toDate ( dirtyDateLeft ) ; const dateRight = toDate ( dirtyDateRight ) ; const sign = compareAsc ( dateLeft , dateRight ) ; const difference = Math . abs ( differenceInCalendarMonths ( dateLeft , dateRight ) ) ; let result ; if ( difference < 1 ) { result = 0 ; } else { if ( dateLeft . getMonth ( ) === 1 && dateLeft . getDate ( ) > 27 ) { dateLeft . setDate ( 30 ) ; } dateLeft . setMonth ( dateLeft . getMonth ( ) - sign * difference ) ; let isLastMonthNotFull = compareAsc ( dateLeft , dateRight ) === - sign ; if ( isLastDayOfMonth ( toDate ( dirtyDateLeft ) ) && difference === 1 && compareAsc ( dirtyDateLeft , dateRight ) === 1 ) { isLastMonthNotFull = false ; } result = sign * ( difference - Number ( isLastMonthNotFull ) ) ; } return result === 0 ? 0 : result ; }