问题描述
我正在尝试更改moment.js设置的日期的语言.默认语言是英语,但是我想设置德语.这些是我尝试过的:
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:
var now = moment().format("LLL").lang("de");
给出NaN
.
var now = moment("de").format("LLL");
这甚至没有反应.
var now = moment().format("LLL", "de");
没有变化:这仍然会产生英文结果.
No change: this is still producing a result in English.
这怎么可能?
推荐答案
您需要moment.lang(警告:lang()
自2.8.0
时刻起就已弃用,请改用locale()
):
You need moment.lang (WARNING: lang()
is deprecated since moment 2.8.0
, use locale()
instead):
moment.lang("de").format('LLL');
http://momentjs.com/docs/#/i18n/
从v2.8.1开始,moment.locale('de')
设置本地化,但不返回moment
.一些例子:
As of v2.8.1, moment.locale('de')
sets the localization, but does not return a moment
. Some examples:
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
,为该实例设置它并返回该实例.
In summation, calling locale
on the global moment
sets the locale for all future moment
instances, but does not return an instance of moment
. Calling locale
on an instance, sets it for that instance AND returns that instance.
另外,正如Shiv在评论中所说,请确保使用"moment-with-locales.min.js"而不是"moment.min.js",否则它将无法正常工作.
Also, as Shiv said in the comments, make sure you use "moment-with-locales.min.js" and not "moment.min.js", otherwise it won't work.
这篇关于如何更改moment.js的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!