import subDays from '../subDays/index' ; import subMonths from '../subMonths/index' ; import type { Duration } from '../types' ; import requiredArgs from '../_lib/requiredArgs/index' ; import toInteger from '../_lib/toInteger/index' ; name category summary description param { Date | Number } date param { Duration } duration returns { Date } throws example export default function sub ( date , 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 dateWithoutMonths = subMonths ( date , months + years * 12 ) ; const dateWithoutDays = subDays ( dateWithoutMonths , days + weeks * 7 ) ; const minutestoSub = minutes + hours * 60 ; const secondstoSub = seconds + minutestoSub * 60 ; const mstoSub = secondstoSub * 1000 ; const finalDate = new Date ( dateWithoutDays . getTime ( ) - mstoSub ) ; return finalDate ; }