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

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

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

Once the date-fns library is installed, you can import the formatISO function in your TypeScript file and use it like this:

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

const date = new Date();
const isoDate = formatISO(date);

console.log(isoDate); // Output: 2021-10-20T11:38:21.431Z
156 chars
7 lines

In the above example, we import the formatISO function from the date-fns library, create a new Date object, and format it using the formatISO function. The resulting isoDate string is then logged to the console.

Note that the formatISO function returns an ISO-8601 formatted date string, which includes the time zone offset in the form of Z (for UTC) or +hh:mm / -hh:mm (for other time zones).

gistlibby LogSnag