15

I get for example: 1day ago. What I need is to set for different language, for example for de. Any suggestion how can I do that?

moment(Date.now()).fromNow()

I tried this:

<script>
    var moment = moment();
    moment.locale('de');
</script>

but get an error:

moment is not a function

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
None
  • 8,817
  • 26
  • 96
  • 171
  • 1
    See [Changing locale globally](http://momentjs.com/docs/#/i18n/changing-locale/) note that you have to import locale data (e.g, [`moment-with-locales.min.js`](https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js) ) – VincenzoC Jan 11 '17 at 16:14

4 Answers4

22

See official docs on how changing locale globally.

Note that you have to import locale data (e.g. moment-with-locales.min.js)

Here a working example:

moment.locale('de');
console.log(moment(Date.now()).fromNow()); //vor ein paar Sekunden
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js"></script>

You can also use only data for a given locale (e.g. de):

moment.locale('de');
console.log(moment(Date.now()).fromNow()); //vor ein paar Sekunden
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/locale/de.js"></script>

Moreover you are redefining moment in your code, that is incorrect

var moment = moment();
VincenzoC
  • 30,117
  • 12
  • 90
  • 112
17

EDIT: In the current version of moment.js this solution still works but it will output a message to the console that states you should use a new method called updateLocale. So the new solution should look like:

Updated solution

import moment from 'moment';
import localization from 'moment/locale/fr';

moment.updateLocale('fr', localization);

Previous Solution

You can change moment locale globally like that:

import moment from 'moment';
import localization from 'moment/locale/fr';

moment.locale('fr', localization);

I hope this helps you.

MikeSli
  • 927
  • 2
  • 14
  • 32
Gapur Kassym
  • 1,131
  • 12
  • 10
  • 2
    this is the only way I have been able to get the latest release to work on react native, installing moment via yarn – ForrestLyman Mar 29 '19 at 02:13
2

To build onto the answer by Gapur, how I got it to work (React native) was like this,

... other imports
import moment from "moment";
import russianLocale from "moment/locale/ru";
import englishLocale from "moment/locale/en-gb";
import chineseLocale from "moment/locale/zh-cn";

const Component = ({ ...props, language }) => {
  moment.locale(language);

... logic

// wherever 'moment' is used, it's displayed in 
// the language that is indicated by the language prop

... other logic

moment version 2.24.0

Mike K
  • 7,621
  • 14
  • 60
  • 120
0

for example I want to use 'fa' locale

import moment from 'moment'
import fa from 'moment/locale/fa'

moment.locale('fa', fa) 
TheEhsanSarshar
  • 2,677
  • 22
  • 41