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

To use the previousFriday function from the date-fns library in TypeScript, first, make sure you have the date-fns library installed in your project:

npm install date-fns
21 chars
2 lines

Then, you can use the previousFriday function in your TypeScript code like this:

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

const today = new Date();
const lastFriday = previousFriday(today);

console.log(lastFriday); // this will output the date object of the previous Friday from today

209 chars
8 lines

In this example, we imported the previousFriday function from the date-fns library and used it to get the date object of the previous Friday from today's date. You can pass any valid date object as an argument to previousFriday function to get the date object of the previous Friday from that date.

Note that you can also use the previousFriday function with a specific date or a string:

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

const specificDate = new Date(2022, 8, 20); // This is September 20th, 2022
const lastFriday = previousFriday(specificDate);

console.log(lastFriday); // This will output the date object of the previous Friday from September 20th, 2022
280 chars
7 lines

This code will output the date object of the previous Friday from September 20th, 2022 in the console.

gistlibby LogSnag