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

To use the issameyear function from date-fns in TypeScript, first install date-fns library using npm:

npm install date-fns
21 chars
2 lines

Then, import the function and call it with two date values:

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

const date1 = new Date('2022-01-01');
const date2 = new Date('2022-08-15');

const isSame = isSameYear(date1, date2);

console.log(isSame); // true
188 chars
9 lines

The isSameYear function returns a boolean indicating whether or not the year of the first date is the same as that of the second date.

You can also use import * as dateFns from 'date-fns'; to import the whole library with all of its functions and then call dateFns.isSameYear(date1, date2);

gistlibby LogSnag