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

To use the endOfYear function from date-fns in JavaScript, first, you need to install the package. You can do that using npm or yarn:

npm install date-fns
21 chars
2 lines

OR

yarn add date-fns
18 chars
2 lines

Once you have installed the package, you can import the endOfYear function in your JavaScript code and use it as follows:

index.tsx
// Importing the endOfYear function from date-fns
import { endOfYear } from 'date-fns';

// Creating a new Date object
const date = new Date('2021-12-20');

// Getting the end of the year of the above date
const endOfYearDate = endOfYear(date);

console.log(endOfYearDate);
// Output: 2021-12-31T23:59:59.999Z
310 chars
12 lines

The endOfYear function takes a Date object or a timestamp as an argument and returns a new Date object representing the end of the year for that input date.

gistlibby LogSnag