react-intl - Custom date formatter

100 Views Asked by At

I would like to specify the date format based on what the user has selected.

When defining the Intl object I can specify the values I would like to include for a date but I can't specify the ordering or exactly how I would like the date formatted.

E.g.

const intl = createIntl({
    locale: 'en',
    messages: messages,
    timeZone: "MST",
    formats: {
        date: {
            custom: {
                day: "2-digit",
                month: "2-digit",
                year: "2-digit",
            }
        }
    }
}, cache)

This formats a date as 01/03/23 (mm/dd/yy).

I would like to format the date based on what the user has selected. Possibly using a library like date-fns.

E.g.

dd/mm/yyyy
mm/dd/yyyy
yyyy/dd/mm
...

How can I format dates based on a date format?

0

There are 0 best solutions below