I am trying to change the language of the date which is being set by moment.js. The default one is English, but I want to set the German language. These is what I tried:
You need moment.lang (WARNING: lang()
is deprecated since moment 2.8.0
, use locale()
instead):
moment.lang("de").format('LLL');
http://momentjs.com/docs/#/i18n/
As of v2.8.1, moment.locale('de')
sets the localization, but does not return a moment
. Some examples:
var march = moment('2017-03')
console.log(march.format('MMMM')) // 'March'
moment.locale('de') // returns the new locale, in this case 'de'
console.log(march.format('MMMM')) // 'March' still, since the instance was before the locale was set
var deMarch = moment('2017-03')
console.log(deMarch.format('MMMM')) // 'März'
// You can, however, change just the locale of a specific moment
march.locale('es')
console.log(march.format('MMMM')) // 'Marzo'
In summation, calling locale
on the global moment
sets the locale for all future moment
instances, but does not return an instance of moment
. Calling locale
on an instance, sets it for that instance AND returns that instance.
Also, as Shiv said in the comments, make sure you use "moment-with-locales.min.js" and not "moment.min.js", otherwise it won't work.
for momentjs 2.12+, do the following:
moment.updateLocale('de');
Also note that you must use moment.updateLocale(localeName, config)
to change an existing locale. moment.defineLocale(localeName, config)
should only be used for creating a new locale.
end 2017 / 2018: the anothers answers have too much old code to edit, so here my alternative clean answer:
with require
let moment = require('moment');
require('moment/locale/fr.js');
// or if you want to include all locales:
require("moment/min/locales.min");
with imports
import moment from 'moment';
import 'moment/locale/fr';
// or if you want to include all locales:
require("moment/min/locales.min");
Use:
moment.locale('fr');
moment().format('D MMM YY'); // Correct, set default global format
// moment.locale('fr').format('D MMM YY') //Wrong old versions for global default format
with timezone
*require:
require('moment-range');
require('moment-timezone');
*import:
import 'moment-range';
import 'moment-timezone';
use zones:
const newYork = moment.tz("2014-06-01 12:00", "America/New_York");
const losAngeles = newYork.clone().tz("America/Los_Angeles");
const london = newYork.clone().tz("Europe/London");
function to format date
const ISOtoDate = function (dateString, format='') {
// if date is not string use conversion:
// value.toLocaleDateString() +' '+ value.toLocaleTimeString();
if ( !dateString ) {
return '';
}
if (format ) {
return moment(dateString).format(format);
} else {
return moment(dateString); // It will use default global format
}
};
FOR METEOR USERS:
moment locales are not installed by default in meteor, you only get the 'en' locale with the default installation.
So you use the code as shown correctly in other answers:
moment.locale('it').format('LLL');
but it will remain in english until you install the locale you need.
There is a nice, clean way of adding individual locales for moment in meteor (supplied by rzymek).
Install the moment package in the usual meteor way with:
meteor add rzymek:moment
Then just add the locales that you need, e.g. for italian:
meteor add rzymek:moment-locale-it
Or if you really want to add all available locales (adds about 30k to your page):
meteor add rzymek:moment-locales
After struggling, this worked for me for moment
v2.26.0:
import React from "react";
import moment from "moment";
import frLocale from "moment/locale/fr";
import esLocale from "moment/locale/es";
export default function App() {
moment.locale('fr', [frLocale, esLocale]) // can pass in 'en', 'fr', or 'es'
let x = moment("2020-01-01 00:00:01");
return (
<div className="App">
{x.format("LLL")}
<br />
{x.fromNow()}
</div>
);
}
You can pass in en
, fr
or es
. If you wanted another language, you'd have to import the locale and add it to the array.
If you only need to support one language it is a bit simpler:
import React from "react";
import moment from "moment";
import "moment/locale/fr"; //always use French
export default function App() {
let x = moment("2020-01-01 00:00:01");
return (
<div className="App">
{x.format("LLL")}
<br />
{x.fromNow()}
</div>
);
}
I am not sure what changed but importing the language file like this worked for me
import 'moment/src/locale/fr';
moment.locale('fr)
Notice the src in the import statement