how to use the formatiso function from date-fns in javascript

To use the formatISO function from date-fns, you will need to install the date-fns package via npm or yarn. Here's an example of how to use it:

index.tsx
const { formatISO } = require('date-fns');

const date = new Date();
const dateString = formatISO(date, { representation: 'date' });
const timeString = formatISO(date, { representation: 'time' });

console.log(dateString); // 2021-12-15
console.log(timeString); // 23:10:01
274 chars
9 lines

The formatISO function takes two arguments: the date object and an options object. The options object is optional and contains a representation property, which can be set to 'date', 'time', or 'complete'.

In the example above, we passed { representation: 'date' } to formatISO, which returns a string representing the date portion of the given date object. We also passed { representation: 'time' } to formatISO, which returns a string representing the time portion of the given date object.

Note that the formatISO function returns the strings in ISO 8601 format, which is a standard format for representing dates and times.

gistlibby LogSnag