how to use the eachweekendofmonth function from date-fns in javascript

The eachWeekendOfMonth function from the date-fns library returns an array containing all weekends within the specified month.

To use the eachWeekendOfMonth function, first, install the date-fns library:

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

Then, import the eachWeekendOfMonth function and pass in a date object that represents the month you want to get the weekend dates from:

index.tsx
import { eachWeekendOfMonth } from 'date-fns';

const date = new Date(2021, 10);

const weekends = eachWeekendOfMonth(date);

console.log(weekends); // [ Sat Nov 06 2021, Sun Nov 07 2021, Sat Nov 13 2021, Sun Nov 14 2021, Sat Nov 20 2021, Sun Nov 21 2021, Sat Nov 27 2021, Sun Nov 28 2021 ]
291 chars
8 lines

In this example, we passed in 2021 as the year, and 10 as the month. Note that months are zero-indexed (January is 0, February is 1, etc.), so 10 represents November.

The eachWeekendOfMonth function returns an array with all weekends in the given month. In this example, it returns an array with all weekend dates in November 2021.

gistlibby LogSnag