MCPcopy
hub / github.com/date-fns/date-fns / add

Function add

pkgs/core/src/add/index.ts:73–103  ·  view source on GitHub ↗
(
  date: DateArg<DateType>,
  duration: Duration,
  options?: AddOptions<ResultDate> | undefined,
)

Source from the content-addressed store, hash-verified

71 * //=> "2017-06-15T15:29:20"
72 */
73export 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}

Callers 2

intervalToDurationFunction · 0.90
test.tsFile · 0.90

Calls 4

toDateFunction · 0.90
addMonthsFunction · 0.90
addDaysFunction · 0.90
constructFromFunction · 0.90

Tested by

no test coverage detected