Javascript 如何更改moment.js的语言?
我正在尝试更改由moment.js设置的日期的语言。默认为英语,但我想设置德语。这就是我所尝试的:Javascript 如何更改moment.js的语言?,javascript,momentjs,Javascript,Momentjs,我正在尝试更改由moment.js设置的日期的语言。默认为英语,但我想设置德语。这就是我所尝试的: var now = moment().format("LLL").lang("de"); 它给出了NaN var now = moment("de").format("LLL"); 这甚至没有反应 var now = moment().format("LLL", "de"); 没有变化:这仍然会在英语中产生结果 这是怎么可能的?您需要moment.lang(警告:lang()已被弃用,因为m
var now = moment().format("LLL").lang("de");
它给出了NaN
var now = moment("de").format("LLL");
这甚至没有反应
var now = moment().format("LLL", "de");
没有变化:这仍然会在英语中产生结果
这是怎么可能的?您需要moment.lang(警告:lang()
已被弃用,因为moment2.8.0
,请改用locale()
):
从v2.8.1开始,
moment.locale('de')
设置本地化,但不返回moment
。一些例子:
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'
总之,在全局moment
上调用locale
设置所有未来moment
实例的区域设置,但不返回moment
的实例。在实例上调用locale
,为该实例设置并返回该实例
另外,正如Shiv在评论中所说的,请确保使用“moment with locales.min.js”而不是“moment.min.js”,否则它将不起作用。您需要moment.lang(警告:lang()
,因为moment2.8.0
,所以不推荐使用locale()
):
从v2.8.1开始,
moment.locale('de')
设置本地化,但不返回moment
。一些例子:
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'
总之,在全局moment
上调用locale
设置所有未来moment
实例的区域设置,但不返回moment
的实例。在实例上调用locale
,为该实例设置并返回该实例
此外,正如Shiv在评论中所说,确保使用“moment with locales.min.js”而不是“moment.min.js”,否则它将无法工作。您需要在脚本中添加
moment.lang(navigator.language)
并且还必须添加您要在其中显示的每个国家/地区:例如,对于GB或FR,您需要在moment.js库中添加该地区格式。momentjs文档中提供了此类格式的示例。如果你不在moment.js中添加这种格式,那么它总是会选择我们的语言环境,因为这是我目前唯一看到的语言环境。你需要在脚本中添加
moment.lang(navigator.language)
并且还必须添加您要在其中显示的每个国家/地区:例如,对于GB或FR,您需要在moment.js库中添加该地区格式。momentjs文档中提供了此类格式的示例。如果您不在momentjs.js中添加此格式,它将始终选择US locale,因为这是我当前唯一看到的格式。使用momentjs 2.8+,请执行以下操作:
moment.locale("de").format('LLL');
moment.updateLocale('de');
使用momentjs 2.8+,执行以下操作:
moment.locale("de").format('LLL');
moment.updateLocale('de');
最快方法:使用Bower安装
我刚刚用bower安装了moment,并将de.js
链接为html项目中的javascript资源
bower安装时刻--保存
您还可以手动下载moment.js
和de.js
在项目中链接“de.js”
链接我的主项目文件中的de.js
,会自动更改对矩类及其方法的所有访问的区域设置
将不再需要执行时刻。locale(“de”)。
或
源代码中的矩.lang(“de”)。
只需如下链接所需的区域设置:
<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/moment/locale/de.js"></script>
或者,如果您通过右键单击下载了moment.js 1990ies样式,则可以在不使用bower\u组件的情况下链接库。最快的方法:使用bower安装
我刚刚用bower安装了moment,并将de.js
链接为html项目中的javascript资源
bower安装时刻--保存
您还可以手动下载moment.js
和de.js
在项目中链接“de.js”
链接我的主项目文件中的de.js
,会自动更改对矩类及其方法的所有访问的区域设置
将不再需要执行时刻。locale(“de”)。
或
源代码中的矩.lang(“de”)。
只需如下链接所需的区域设置:
<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/moment/locale/de.js"></script>
或者,如果您通过右键单击下载了moment.js 1990ies样式,则可以在不使用bower\u组件的情况下链接库,这在大多数情况下仍然可以正常工作。对于METEOR用户:
瞬间meteor中默认不安装语言环境,您只会获得默认安装的“en”语言环境
因此,您正确使用了其他答案中所示的代码:
moment.locale('it').format('LLL');
但在您安装所需的区域设置之前,它将一直使用英语
在meteor(由rzymek提供)中,有一种很好的、干净的方法可以为瞬间添加单独的区域设置
以通常的meteor方式安装moment软件包,包括:
meteor add rzymek:moment
然后只需添加您需要的地区,例如意大利语:
meteor add rzymek:moment-locale-it
或者,如果您确实想添加所有可用的区域设置(在页面中添加大约30k):
对于METEOR用户:
瞬间meteor中默认不安装语言环境,您只会获得默认安装的“en”语言环境
因此,您正确使用了其他答案中所示的代码:
moment.locale('it').format('LLL');
但在您安装所需的区域设置之前,它将一直使用英语
在meteor(由rzymek提供)中,有一种很好的、干净的方法可以为瞬间添加单独的区域设置
以通常的meteor方式安装moment软件包,包括:
meteor add rzymek:moment
然后只需添加您需要的地区,例如意大利语:
meteor add rzymek:moment-locale-it
或者,如果您确实想添加所有可用的区域设置(在页面中添加大约30k):
哎呀,笔误。我要解决这个问题:
var矩=函数(x){返回矩(x).locale('
alert(moment(date).fromNow())
moment.locale("de");
var m = moment().format("LLL")
let moment = require('moment');
require('moment/locale/fr.js');
// or if you want to include all locales:
require("moment/min/locales.min");
import moment from 'moment';
import 'moment/locale/fr';
// or if you want to include all locales:
require("moment/min/locales.min");
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
require('moment-range');
require('moment-timezone');
import 'moment-range';
import 'moment-timezone';
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");
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
}
};
moment.locale('de');
var now = moment();
now.format('LLL');
await import('moment/locale/en-ca');
moment.locale('en-ca');
moment.locale('en-ca');
await import('moment/locale/en-ca');
import 'moment/src/locale/fr';
moment.locale('fr)
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>
);
}
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>
);
}