mazey
    Preparing search index...

    Function subYears

    • Subtract a number of calendar years from a date.

      Positive decimal amounts are rounded with Math.floor; negative decimal amounts are rounded with Math.ceil. A negative amount therefore adds calendar years. When the destination year does not contain the original day, the result is clamped to the final day of that month.

      Usage:

      import { formatDate, subYears } from "mazey";

      const result = subYears(new Date(2014, 8, 1), 5);
      console.log(formatDate(result, "yyyy-MM-dd"));

      Output:

      2009-09-01
      

      Parameters

      • date: number | Date

        Date or timestamp in milliseconds to change.

      • amount: number

        Number of years to subtract. Positive decimals use Math.floor; negative decimals use Math.ceil.

      Returns Date

      A new Date with the specified number of calendar years subtracted. Invalid inputs produce an invalid Date.

      The calculation uses local calendar fields and preserves the local month, time, and day where possible. The supplied Date is not mutated.