UNPKG

874 BJavaScriptView Raw
1import { addYears } from "./addYears.mjs";
2
3/**
4 * @name subYears
5 * @category Year Helpers
6 * @summary Subtract the specified number of years from the given date.
7 *
8 * @description
9 * Subtract the specified number of years from the given date.
10 *
11 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
12 *
13 * @param date - The date to be changed
14 * @param amount - The amount of years to be subtracted.
15 *
16 * @returns The new date with the years subtracted
17 *
18 * @example
19 * // Subtract 5 years from 1 September 2014:
20 * const result = subYears(new Date(2014, 8, 1), 5)
21 * //=> Tue Sep 01 2009 00:00:00
22 */
23export function subYears(date, amount) {
24 return addYears(date, -amount);
25}
26
27// Fallback for modularized imports:
28export default subYears;