import addDays from '../addDays/index' ; import addMonths from '../addMonths/index' ; import toDate from '../toDate/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; import toInteger from '../_lib/toInteger/index' ; import type { Duration } from '../types' ; name category summary description param { Date | Number } date param { Duration } duration returns { Date } throws example export default function add ( dirtyDate , duration ) { requiredArgs ( 2 , arguments ) ; if ( ! duration || typeof duration !== 'object' ) return new Date ( NaN ) ; const years = duration . years ? toInteger ( duration . years ) : 0 ; const months = duration . months ? toInteger ( duration . months ) : 0 ; const weeks = duration . weeks ? toInteger ( duration . weeks ) : 0 ; const days = duration . days ? toInteger ( duration . days ) : 0 ; const hours = duration . hours ? toInteger ( duration . hours ) : 0 ; const minutes = duration . minutes ? toInteger ( duration . minutes ) : 0 ; const seconds = duration . seconds ? toInteger ( duration . seconds ) : 0 ; const date = toDate ( dirtyDate ) ; const dateWithMonths = months || years ? addMonths ( date , months + years * 12 ) : date ; const dateWithDays = days || weeks ? addDays ( dateWithMonths , days + weeks * 7 ) : dateWithMonths ; const minutesToAdd = minutes + hours * 60 ; const secondsToAdd = seconds + minutesToAdd * 60 ; const msToAdd = secondsToAdd * 1000 ; const finalDate = new Date ( dateWithDays . getTime ( ) + msToAdd ) ; return finalDate ; }