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

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

index.ts
npm install date-fns
21 chars
2 lines

Then, import the format function and use it to format a date string:

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

const date = new Date();
const formattedDate = format(date, 'yyyy-MM-dd');
console.log(formattedDate);
139 chars
6 lines

This will output the date in the format of "YYYY-MM-DD". You can customize the format to your needs by modifying the string passed as the second argument to the format function. For example, to include the time, you could use:

index.ts
const formattedDateTime = format(date, 'yyyy-MM-dd HH:mm:ss');
console.log(formattedDateTime);
95 chars
3 lines

For more information on date formatting options, see the date-fns documentation: https://date-fns.org/v2.16.1/docs/format

gistlibby LogSnag