24

My project is a react project.

My website is a mutilanguage website, when I change the web language. moment.locale(lang) not working.

My code is:

const startDate = moment.utc(start).locale(lang);
const endDate = moment.utc(end).locale(lang);

whatever I set lang I check the startDate.locale() always is 'en' startDate.format('ll') result always is English.

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
Yang Wang
  • 375
  • 1
  • 2
  • 6
  • Possible duplicate of [how do I change the language of moment.js](https://stackoverflow.com/questions/17493309/how-do-i-change-the-language-of-moment-js) – VincenzoC Apr 12 '18 at 08:10
  • 1
    same issue here did you got the fix yet? – Omer May 28 '18 at 15:20
  • Possible duplicate of [How to change locale in momentJS?](https://stackoverflow.com/questions/48530110/how-to-change-locale-in-momentjs) – blalasaadri Feb 12 '19 at 12:20

5 Answers5

69

If the project was created using create-react-app, moment locales were probably excluded by default.

This is now documented in the "Moment.js locales are missing" section of create-react-app's troubleshooting guide.

Solution: explicitly import locales in addition to 'moment':

import moment from 'moment';
import 'moment/locale/fr';
import 'moment/locale/es';
// etc. as required
searlea
  • 8,173
  • 4
  • 34
  • 37
20

According to this github issue, from 2022 on or so, it has to be imported like so:

import moment from 'moment';
import 'moment/dist/locale/de';

moment.locale('de');

if this doesn't work, try this change:

import moment from 'moment/dist/moment';
Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132
1

I think if you do

import 'moment/min/locales'

Instead of individual import of each locale. In my case it resolve my problem

Dhaval Parmar
  • 241
  • 1
  • 3
  • 13
0

I found the solution here: https://stackoverflow.com/a/55334751/8318855

You should use the moment.updateLocale function

batatop
  • 979
  • 2
  • 14
  • 31
0

For a react native app importing moment like this fixed the issue

import moment from 'moment/min/moment-with-locales';

reference

Gabriel Aguirre
  • 589
  • 4
  • 9