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

To use the differenceInMinutes() function from the date-fns library in TypeScript, first, we need to install the library by running the following command.

npm install date-fns
21 chars
2 lines

Then, we import the differenceInMinutes() function in our TypeScript file like this:

index.ts
import { differenceInMinutes } from 'date-fns';
48 chars
2 lines

After that, we can use the differenceInMinutes() function to find the difference between two dates in minutes. Here's how we can use it:

index.ts
const date1 = new Date('2021-07-01T10:00:00');
const date2 = new Date('2021-07-01T10:30:00');

const difference = differenceInMinutes(date1, date2);
console.log(`The difference between ${date1} and ${date2} is ${difference} minutes`);
235 chars
6 lines

This will output:

index.ts
The difference between Thu Jul 01 2021 10:00:00 GMT+0530 (India Standard Time) and Thu Jul 01 2021 10:30:00 GMT+0530 (India Standard Time) is -30 minutes
154 chars
2 lines

Note that the differenceInMinutes() function returns a negative value if date1 is after date2. To get a positive value, simply reverse the order of the arguments.

gistlibby LogSnag