how to use the formatdistance function from the date-fns library in typescript

To use the formatDistance function from the date-fns library in TypeScript, first you need to install the library:

npm install date-fns
21 chars
2 lines

Then, import the function in your TypeScript file:

index.ts
import { formatDistance } from 'date-fns';
43 chars
2 lines

Now you can use the formatDistance function to format the distance between two dates:

index.ts
const distanceInWords = formatDistance(
  new Date(2020, 0, 1),
  new Date(2020, 0, 15),
  { addSuffix: true }
);

console.log(distanceInWords); // prints "in 2 weeks"
168 chars
8 lines

In this example, the formatDistance function is used to format the distance between January 1st and January 15th, 2020. The addSuffix option is set to true, which adds the word "in" before the formatted distance.

gistlibby LogSnag