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:
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 (
{x.format("LLL")}
{x.fromNow()}
);
}
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 (
{x.format("LLL")}
{x.fromNow()}
);
}