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

To use the lastDayOfYear function from date-fns, you first need to install the package. You can do this using npm by running the following command:

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

Once you have the package installed, you can import the lastDayOfYear function and use it in your code:

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

const date = new Date('2021-01-01');
const lastDay = lastDayOfYear(date);

console.log(lastDay); // Output: 2021-12-31T05:00:00.000Z
176 chars
7 lines

In the example above, we create a new Date object representing January 1st, 2021. We then pass this object to the lastDayOfYear function, which returns a new Date object representing the last day of the year (December 31st, 2021 in this case). Finally, we log the result to the console.

Note that the lastDayOfYear function returns a Date object in UTC, so the output may differ depending on your timezone.

gistlibby LogSnag