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

To use the subyears() function from the date-fns library in TypeScript, you should first install the library using npm. You can do this by running the following command in your terminal:

index.ts
npm install date-fns
21 chars
2 lines

Once installed, you can import the subYears() function and use it as shown below:

index.ts
import { subYears } from 'date-fns';

const currentDate = new Date();
const fiveYearsAgo = subYears(currentDate, 5);

console.log(fiveYearsAgo);    // Output: 2016-11-02T09:46:26.236Z
184 chars
7 lines

In the above code, we first import the subYears() function from the date-fns library. We then create a currentDate variable initialized with the current date. Next, we use the subYears() function to subtract 5 years from the currentDate and store the result in the fiveYearsAgo variable. Finally, we log the fiveYearsAgo variable to the console, which gives us the date 5 years ago from the current date.

gistlibby LogSnag