我正在尝试更改moment.js设置的日期的语言。默认是英语,但我想设置德语。以下是我的尝试:
var now = moment().format("LLL").lang("de");
它会产生NaN。
var now = moment("de").format("LLL");
这甚至不是反应。
var now = moment().format("LLL", "de");
没有变化:这仍然是用英语生成的结果。
这怎么可能呢?
我正在尝试更改moment.js设置的日期的语言。默认是英语,但我想设置德语。以下是我的尝试:
var now = moment().format("LLL").lang("de");
它会产生NaN。
var now = moment("de").format("LLL");
这甚至不是反应。
var now = moment().format("LLL", "de");
没有变化:这仍然是用英语生成的结果。
这怎么可能呢?
当前回答
当我与gulp和朋友一起使用webpack时(这个生成器为我设置了一切),我不得不对凉亭进行更改。json文件。我必须重写当前包的默认导入,并选择所有语言附带的文件:
"overrides": {
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
}
这是我的凉亭。json文件:
{
"name": "html5",
"version": "0.0.0",
"dependencies": {
"angular-animate": "~1.4.2",
"angular-cookies": "~1.4.2",
"angular-touch": "~1.4.2",
"angular-sanitize": "~1.4.2",
"angular-messages": "~1.4.2",
"angular-ui-router": "~0.2.15",
"bootstrap-sass": "~3.3.5",
"angular-bootstrap": "~0.13.4",
"malarkey": "yuanqing/malarkey#~1.3.1",
"angular-toastr": "~1.5.0",
"moment": "~2.10.6",
"animate.css": "~3.4.0",
"angular": "~1.4.2",
"lodash": "^4.13.1",
"angular-moment": "^0.10.3",
"angularLocalStorage": "ngStorage#^0.3.2",
"ngstorage": "^0.3.10"
},
"devDependencies": {
"angular-mocks": "~1.4.2"
},
"overrides": {
"bootstrap-sass": {
"main": [
"assets/stylesheets/_bootstrap.scss",
"assets/fonts/bootstrap/glyphicons-halflings-regular.eot",
"assets/fonts/bootstrap/glyphicons-halflings-regular.svg",
"assets/fonts/bootstrap/glyphicons-halflings-regular.ttf",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff2"
]
},
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
},
"resolutions": {
"angular": "~1.4.2"
}
}
其他回答
返回moment(status.created_at).locale('es').fromNow();
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>
我们可以使用moment.locale()方法来传递您自己的语言ex。我使用window.navigator.language (ex.en- us)在运行时传递lange。
const formatDate = date => moment(date).locale(window.navigator.language).format('LL')
在2.18.1秒之后:
moment.locale("de");
var m = moment().format("LLL")
对于那些在异步环境中工作的人来说,在按需加载区域设置时,moment的行为会出乎意料。
而不是
await import('moment/locale/en-ca');
moment.locale('en-ca');
颠倒顺序
moment.locale('en-ca');
await import('moment/locale/en-ca');
似乎locale被加载到当前选定的locale中,覆盖了之前设置的任何locale信息。所以先切换语言环境,再加载语言环境信息不会导致这个问题。