Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 2x 2x 2x 52x 50x 50x 50x 50x 50x 50x 50x 50x 52x 50x 50x 50x 50x | import { DurationInput, DateInput } from './types';
import { addMonths } from './lib/dateUtils';
import { parse } from './parse';
/**
* Return a new date with the duration applied.
*
* @example
* const newDate = apply('2020-01-01T00:00:00.000Z', { years: 2 })
* newDate.toISOString() // '2022-01-01T00:00:00.000Z'
*/
export const apply = (
date: DateInput,
duration: DurationInput,
): Date => {
const parsedDate = new Date(date);
const {
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
} = parse(duration);
addMonths(parsedDate, (years * 12) + months);
parsedDate.setDate(parsedDate.getDate() + (weeks * 7) + days);
parsedDate.setHours(
parsedDate.getHours() + hours,
parsedDate.getMinutes() + minutes,
parsedDate.getSeconds() + seconds,
parsedDate.getMilliseconds() + milliseconds,
);
return parsedDate;
};
|