how to use the subisoweekyears function from the date-fns library in typescript

To use the subISOWeekYears function from the date-fns library in TypeScript, you can follow the below steps.

  1. Install date-fns library as a dependency in your project using npm or Yarn.
npm install date-fns
21 chars
2 lines

or

yarn add date-fns
18 chars
2 lines
  1. Import the subISOWeekYears function in your TypeScript file where you want to use it.
import { subISOWeekYears } from 'date-fns';
44 chars
2 lines
  1. Use the subISOWeekYears function to subtract the specified number of ISO week-numbering years from the given date.
const myDate = new Date('2022-07-01');
const yearsToSubtract = 2;

const newDate = subISOWeekYears(myDate, yearsToSubtract);

console.log(newDate); // Output: 2020-07-03T00:00:00.000Z
184 chars
7 lines

In the above example, we are subtracting 2 ISO week-numbering years from the date 2022-07-01 and the output is 2020-07-03T00:00:00.000Z. The subISOWeekYears function takes two arguments - the first argument is the date to subtract years from, and the second argument is the number of ISO week-numbering years to subtract.

gistlibby LogSnag