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

To use the min function from the date-fns library in JavaScript, you first need to install the date-fns package from npm. You can do this by running the following command in your terminal:

npm install date-fns
21 chars
2 lines

After installing the package, you can use the min function to find the minimum date in an array of dates. The min function takes an array of dates as its argument and returns the minimum date:

index.tsx
const { min } = require('date-fns');

const dates = [
  new Date('2020-01-01'),
  new Date('2021-01-01'),
  new Date('2022-01-01')
];

const minimumDate = min(dates);

console.log(minimumDate); // Output: Wed Jan 01 2020 00:00:00 GMT-0800 (Pacific Standard Time)
263 chars
12 lines

In the code above, we first import the min function from the date-fns library using destructuring assignment. We then create an array of dates and pass that array to the min function. The minimumDate variable contains the minimum date in the array, which we log to the console.

gistlibby LogSnag