I use the esm version of date-fns and you can use the same type of formats that moment uses :
import { format } from 'date-fns/esm'
import { enUS, fr } from 'date-fns/esm/locale'
I'll store the locales in an object :
this.dateLocales = { fr: fr, en: enUS }
and use these formats :
LT: 'h:mm aa',
LTS: 'h:mm:ss aa',
L: 'MM/DD/YYYY',
LL: 'MMMM D YYYY',
LLL: 'MMMM D YYYY h:mm aa',
LLLL: 'dddd, MMMM D YYYY h:mm aa'
So you can do :
format(
new Date(2014, 6, 2),
'LL',
{locale: this.dateLocales.fr}
)
Those formats are localised
Answer from Lakston on Stack OverflowI use the esm version of date-fns and you can use the same type of formats that moment uses :
import { format } from 'date-fns/esm'
import { enUS, fr } from 'date-fns/esm/locale'
I'll store the locales in an object :
this.dateLocales = { fr: fr, en: enUS }
and use these formats :
LT: 'h:mm aa',
LTS: 'h:mm:ss aa',
L: 'MM/DD/YYYY',
LL: 'MMMM D YYYY',
LLL: 'MMMM D YYYY h:mm aa',
LLLL: 'dddd, MMMM D YYYY h:mm aa'
So you can do :
format(
new Date(2014, 6, 2),
'LL',
{locale: this.dateLocales.fr}
)
Those formats are localised
As of 2021, using currently latest version of date-fns (v2.23.0), you can achieve what you want by using the 'P' format.
See: https://date-fns.org/v2.23.0/docs/format
For example, given that today is 2021-08-27 (ISO date):
import { format } from 'date-fns';
import ptBrLocale from 'date-fns/locale/pt-BR';
import enUsLocale from 'date-fns/locale/en-US';
console.log(format(new Date(), 'P', { locale: ptBrLocale }));
console.log(format(new Date(), 'P', { locale: enUsLocale }));
Outputs will be:
27/08/2021
08/27/2021
» npm install date-fns
As you are using the FP version to format a date you need to use the formatWithOptions function to pass the locale options.
import format from "date-fns/fp/formatWithOptions";
It has a slightly different function signature then the format function, where you need to pass the options as the first argument.
const formatMonth = format({ locale: de }, "MMM");
You need to pass the Date as the first Parameter. For an example,
const formatMonth = format(new Date(),"MMM", {locale: de}); <br>
OR
const formatMonth = format(new Date(10/28/2021),"MMM", {locale: de});

» npm install date-fns-tz
This has been released in version 2.29: https://date-fns.org/v2.29.0/docs/setDefaultOptions
// Set global locale:
var setDefaultOptions = require('date-fns/setDefaultOptions')
import { es } from 'date-fns/locale'
setDefaultOptions({ locale: es })
As I know, there is no such option. Usually I create custom wrapper function around formatDate functions and pass there application locale. You could store locale in global variables or in app level stores:
formatRelativeWrap.js
import { formatRelative } from 'date-fns'
import AppStore from 'appStore'
export default (date1, date2, locale) => {
return formatRelative(date1, date2, { locale: locale || AppStore.defaultLocale})
}
As @Pointy mentioned there is note about this in official docs - https://date-fns.org/v2.22.1/docs/I18n - second example.
Although date-fns does not support a native method to do partial overwrites (yet), you can do the following, to do some manual tweaking (shown here by the german locale):
import { formatRelative } from 'date-fns';
import { de } from 'date-fns/esm/locale';
const formatRelativeLocale = {
lastWeek: '[letzten] dddd [um] LT',
yesterday: '[gestern um] LT',
today: '[heute um] LT',
tomorrow: '[morgen um] LT',
nextWeek: 'dddd [um] LT',
other: 'L LT', // Difference: Add time to the date
};
const locale = {
...de,
formatRelative: token => formatRelativeLocale[token],
};
const text = formatRelative(date, new Date(), { locale });
An update on the accepted answer: it looks like in the latest version of date-fns, the format strings look slightly different (single quotes instead of braces):
import { formatRelative } from 'date-fns';
import { de } from 'date-fns/esm/locale';
const formatRelativeLocale = {
lastWeek: "'letzten' dddd 'um' LT",
yesterday: "'gestern um' LT",
today: "'heute um' LT",
tomorrow: "'morgen um' LT",
nextWeek: "dddd 'um' LT",
other: 'L LT', // Difference: Add time to the date
};
const locale = {
...de,
formatRelative: token => formatRelativeLocale[token],
};
const text = formatRelative(date, new Date(), { locale });
Also see this example of the format string specified in the date-fns source.