import toInteger from '../_lib/toInteger/index' ; import toDate from '../toDate/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; name category summary description param { Date | Number } date param { Number } amount returns { Date } throws example export default function addMonths ( dirtyDate , dirtyAmount ) { requiredArgs ( 2 , arguments ) ; const date = toDate ( dirtyDate ) ; const amount = toInteger ( dirtyAmount ) ; if ( isNaN ( amount ) ) { return new Date ( NaN ) ; } if ( ! amount ) { return date ; } const dayOfMonth = date . getDate ( ) ; const endOfDesiredMonth = new Date ( date . getTime ( ) ) ; endOfDesiredMonth . setMonth ( date . getMonth ( ) + amount + 1 , 0 ) ; const daysInMonth = endOfDesiredMonth . getDate ( ) ; if ( dayOfMonth >= daysInMonth ) { return endOfDesiredMonth ; } else { date . setFullYear ( endOfDesiredMonth . getFullYear ( ) , endOfDesiredMonth . getMonth ( ) , dayOfMonth ) ; return date ; } }