( date: DateArg<DateType>, duration: Duration, options?: AddOptions<ResultDate> | undefined, )
| 71 | * //=> "2017-06-15T15:29:20" |
| 72 | */ |
| 73 | export function add<DateType extends Date, ResultDate extends Date = DateType>( |
| 74 | date: DateArg<DateType>, |
| 75 | duration: Duration, |
| 76 | options?: AddOptions<ResultDate> | undefined, |
| 77 | ): ResultDate { |
| 78 | const { |
| 79 | years = 0, |
| 80 | months = 0, |
| 81 | weeks = 0, |
| 82 | days = 0, |
| 83 | hours = 0, |
| 84 | minutes = 0, |
| 85 | seconds = 0, |
| 86 | } = duration; |
| 87 | |
| 88 | // Add years and months |
| 89 | const _date = toDate(date, options?.in); |
| 90 | const dateWithMonths = |
| 91 | months || years ? addMonths(_date, months + years * 12) : _date; |
| 92 | |
| 93 | // Add weeks and days |
| 94 | const dateWithDays = |
| 95 | days || weeks ? addDays(dateWithMonths, days + weeks * 7) : dateWithMonths; |
| 96 | |
| 97 | // Add days, hours, minutes, and seconds |
| 98 | const minutesToAdd = minutes + hours * 60; |
| 99 | const secondsToAdd = seconds + minutesToAdd * 60; |
| 100 | const msToAdd = secondsToAdd * 1000; |
| 101 | |
| 102 | return constructFrom(options?.in || date, +dateWithDays + msToAdd); |
| 103 | } |
no test coverage detected