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

To use the previousWednesday function from the date-fns library in TypeScript, first make sure that you have installed the library by running npm i date-fns in the project directory.

Then, you can import the previousWednesday function as follows:

index.ts
import { previousWednesday } from 'date-fns';
46 chars
2 lines

You can then use the function with a Date object:

index.ts
const date = new Date(2022, 1, 9); // Wednesday, February 9 2022
const prevWed = previousWednesday(date); // Wednesday, February 2 2022
136 chars
3 lines

Alternatively, you can use the function with a Unix timestamp:

index.ts
const timestamp = 1644405600000; // Wednesday, February 9 2022
const prevWed = previousWednesday(timestamp); // Wednesday, February 2 2022
139 chars
3 lines

Note that if you're using ECMAScript modules, you may need to set "esModuleInterop": true in your tsconfig.json file to import the previousWednesday function correctly.

gistlibby LogSnag