Cristian Flórez
Cristian Flórez

Reputation: 2781

Format date according user locale with date-fns

I'm using date-fns and I would like to display dates as per user’s selected locale (language+region) for example: “Jan 12, 2021” for en_US or “12 Jan 2021.” for en_CA., There is a way to achieve this with date-fns?

Upvotes: 2

Views: 7249

Answers (1)

morganney
morganney

Reputation: 13580

The P patterns to format deal with localization: https://date-fns.org/v2.25.0/docs/format

Looks like you would want the PP variant and can be used like this:

import { caLocale } from 'date-fns/locale/ca'

const result = format(new Date(2021, 0, 12), "PP", {
  locale: caLocale
})

Upvotes: 2

Related Questions