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

You can use the lastDayOfMonth function from the date-fns library in JavaScript to get the last day of a given month.

First, you need to install the date-fns library using npm or yarn:

npm install date-fns

# or, with yarn
yarn add date-fns
56 chars
5 lines

Then, you can import the lastDayOfMonth function and use it like this:

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

const date = new Date('2022-02-11'); // February 11, 2022
const lastDay = lastDayOfMonth(date); // February 28, 2022

console.log(lastDay); // output: 2022-02-28T00:00:00.000Z
220 chars
7 lines

In the code above, we first import the lastDayOfMonth function from the date-fns library. Then, we create a new Date object for February 11, 2022. Finally, we call the lastDayOfMonth function with the date object, which returns the last day of the month (February 28, 2022 in this case).

gistlibby LogSnag