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

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

它会产生NaN。

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

这甚至不是反应。

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

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

这怎么可能呢?


当前回答

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

其他回答

对于momentjs 2.12+,执行以下操作:

moment.updateLocale('de');

还要注意,你必须使用moment。updateLocale(localeName, config)来更改现有的语言环境。moment.defineLocale(localeName, config)应该只用于创建一个新的区域设置。

我使用的是angular2-moment,但用法必须相似。

import { MomentModule } from "angular2-moment";
import moment = require("moment");

export class AppModule {

  constructor() {
    moment.locale('ru');
  }
}

最快的方法:使用Bower安装

我刚刚在我的html项目中安装了moment with bower和链接de.js作为javascript资源。

凉亭安装时刻—保存

您也可以手动下载moment.js和de.js。

在项目中链接“de.js”

在我的主项目文件中链接de.js会自动改变对moment类及其方法的所有访问的语言环境。

不再需要执行moment.locale("de")。或 moment.lang(“德”)。在源代码中。

只需像这样链接你想要的地区:

<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/moment/locale/de.js"></script>

或者,如果您通过右键单击下载了moments .js 1990ies样式,则可以在没有bower_components路径的情况下链接库,这在大多数情况下仍然可以正常工作。

<!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

根据版本更改moment js语言

版本:2 + 8。

moment.locale('嗨');

版本:2.5.1

moment.lang('嗨');