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

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

Step 1: First, install the date-fns library by running the following command from the terminal:

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

Step 2: Import the previousthursday function from the date-fns library in your TypeScript file:

index.ts
import { previousthursday } from 'date-fns';
45 chars
2 lines

Step 3: Use the previousthursday function in your code to get the date object of the previous Thursday from a given date:

index.ts
const date = new Date('2022-01-01');
const previousThursday = previousthursday(date); // Thu, 30 Dec 2021 00:00:00 GMT
119 chars
3 lines

In the above code, we first create a Date object for 1st January 2022. We then pass this date to the previousthursday function which returns a date object for the previous Thursday i.e. 30th December 2021.

gistlibby LogSnag