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

To use the lastDayOfWeek function from date-fns library in TypeScript, you first need to install the library using your preferred package manager:

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

or

index.ts
yarn add date-fns
18 chars
2 lines

Then, you can import and use the function as shown below:

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

const date = new Date('2022-10-10');
const lastDayOfWeekDate = lastDayOfWeek(date, { weekStartsOn: 1 }); // {Sun Oct 16 2022 00:00:00 GMT+0530 (India Standard Time)}
209 chars
5 lines

In the above example, we first import the lastDayOfWeek function from the date-fns library. Then, we create a new Date object with a specific date (in this case, 10th October 2022). Finally, we call the lastDayOfWeek function with the date object and an options object that specifies which day of the week is considered the start of the week (in this case, Monday). The function returns the last day of the week for the given date (in this case, 16th October 2022).

gistlibby LogSnag