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

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

npm i date-fns
15 chars
2 lines

After installing the library, you can import the differenceInCalendarYears function and use it like this:

index.ts
import { differenceInCalendarYears } from 'date-fns';

const date1 = new Date(2010, 5, 15);
const date2 = new Date(2014, 3, 28);
const diff = differenceInCalendarYears(date2, date1);

console.log(diff); // Output: 3
216 chars
8 lines

In the example code above, we imported the differenceInCalendarYears function from the date-fns library and used it to calculate the difference in calendar years between two dates. The diff variable holds the result of the calculation, which is the number of full calendar years between date2 and date1.

gistlibby LogSnag