我正在尝试更改moment.js设置的日期的语言。默认是英语,但我想设置德语。以下是我的尝试:

var now = moment().format("LLL").lang("de");

它会产生NaN。

var now = moment("de").format("LLL");

这甚至不是反应。

var now = moment().format("LLL", "de");

没有变化:这仍然是用英语生成的结果。

这怎么可能呢?


当前回答

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>MomentJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script type="text/javascript" src="moment.js"></script>
    <script type="text/javascript" src="locale/ne.js"></script>
</head>
<body>
    <script>
        jQuery(document).ready(function($) {
            moment.locale('en'); // default the locale to English
            var localLocale = moment();

            moment.locale('ne'); // change the global locale to Nepalese
            var ne1 = localLocale.format('LLLL');
            var ne2 = moment().format('LLLL');

            $('.ne1').text(ne1);
            $('.ne2').text(ne2);
        });
    </script>
    <p class="ne1"></p>
    <p class="ne2"></p>
</body>
</html>

Demo

其他回答

哎呀,笔误。我会解决这个问题: Var矩=函数(x){返回矩(x).locale('de');其他方法在某些条件下(对我来说)似乎不太管用。

您需要在脚本中添加moment.lang(navigator.language)。

并且必须添加你想要显示的每个国家的语言环境:例如GB或FR,你需要在moment.js库中添加该语言环境格式。在momentjs文档中可以找到这种格式的示例。如果你不在moment.js中添加这种格式,那么它总是会选择US locale,因为这是我目前看到的唯一一个。

我还必须导入语言:

import moment from 'moment'
import 'moment/locale/es'  // without this line it didn't work
moment.locale('es')

然后像平常一样利用这一刻

console.log(moment(date).fromNow())

经过一番挣扎后,这对我来说是有效的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>
  );
}

你可以传入en fr es。如果需要另一种语言,则必须导入区域设置并将其添加到数组中。

如果你只需要支持一种语言,那就更简单了:

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>
  );
}

——更新——

在一种情况下,像上面那样导入所有区域设置文件将使用最后导入的始终使用的区域设置(即。在上面的例子中是“es”),即使我把它设置为“fr”。似乎有效的解决方案是:

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

...

moment.locale('fr');

First Call, p5.js和moment-with-locale .js,然后像下面这样做代码,你会得到你的结果。

在这个结果中,我用不同的语言显示了月份名称:)

请检查代码:

var monthNameEnglish = moment().locale('en-gb').format('MMMM'); document.getElementById('monthNameEnglish').innerHTML = monthNameEnglish; var monthNameGerman = moment().locale('de').format('MMMM'); document.getElementById('monthNameGerman').innerHTML = monthNameGerman; <!DOCTYPE html> <html> <head> <title>P5.js and Moment.js</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment-with-locales.js"></script> <h3>English Version Month Name</h3> <p id="monthNameEnglish"></p> <h3> German Version Month Name</h3> <p id="monthNameGerman"></p> </head> <body> </body> </html>