React Native moment.locale does not work

Viewed 13011

I am working with React Native version 0.45.1 and with moment version 2.18.1.

I am trying to change the date according to the device local, but I always get the date in 'en-Us' locale. I can't import all of the locales as I saw in some solutions since I don't know the device locale in advance. (for example: https://github.com/moment/moment/issues/2962)

any other options?

2 Answers

Instead of importing locale by locale, I'm using this solution to set the locale globally:

import { getDeviceLocale } from "react-native-device-info";
import moment from "moment";
import "moment/min/locales";

const deviceLocale = getDeviceLocale();

moment.locale(deviceLocale); //set your locale (eg: fr)
moment(1316116057189).fromNow(); // il y a 7 ans

Sharing this to those who need it

Related