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");
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});

moment vs date-fns locale date formats
javascript - Failing to "dynamically" import date-fns/locale libs - TypeScript giving an attempted import error - Stack Overflow
Link to list of locales
Format date according user locale with date-fns
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
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
Here's the code I am using for doing dynamic lookups using Expo's Localization object.
import * as Localization from 'expo-localization';
import * as Locales from 'date-fns/locale';
import { Locale } from 'date-fns';
/**
* Looks up a date-fns locale from the Expo localization object. This falls back to `en-US`
* @param localization Expo Localization object containing the locale and region.
* @returns date-fns locale.
*/
export function getDateFnsLocale({ locale, region }: Pick<typeof Localization, 'locale'|'region'>) : Locale {
return (
Locales[locale.substring(0, 2) + region] ?? Locales[locale.substring(0, 2)] ?? Locales.enUS
);
}
Here's the test
import { enUS, fr, frCA } from 'date-fns/locale';
describe('date-fns locale lookup', () => {
it('shound find fr', () => {
expect(getDateFnsLocale({ locale: 'fr', region: null })).toBe(fr);
});
it('shound find fr-CA', () => {
expect(getDateFnsLocale({ locale: 'fr-CA', region: 'CA' })).toBe(frCA);
});
it('shound not find zz-ZZ', () => {
expect(getDateFnsLocale({ locale: 'zz-ZZ', region: 'ZZ' })).toBe(enUS);
});
});
this is my solution
import * as loc from 'date-fns/locale';
export const getDateFnsLocaleByActiveLanguage = (lang: string) =>
lang === 'en'
? loc['enUS']
: Object.values(loc).find((l) => l.code === lang);
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.
» npm install date-fns-tz