我正在尝试更改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");
没有变化:这仍然是用英语生成的结果。
这怎么可能呢?
当前回答
最快的方法:使用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路径的情况下链接库,这在大多数情况下仍然可以正常工作。
其他回答
在2.18.1秒之后:
moment.locale("de");
var m = moment().format("LLL")
返回moment(status.created_at).locale('es').fromNow();
要使用moment(2.8.0之后的版本)更改区域设置,请执行以下步骤。
在index.html中按如下方式加载moment语言环境文件 < script src = " . . / node_modules / /地区/ it.js”> > < /脚本 根据需要设置locale - moment.locale('it'); moment。locale()会返回it 你可以在任何语言中使用moment,比如JavaScript、Angular、node等等。
对于那些在异步环境中工作的人来说,在按需加载区域设置时,moment的行为会出乎意料。
而不是
await import('moment/locale/en-ca');
moment.locale('en-ca');
颠倒顺序
moment.locale('en-ca');
await import('moment/locale/en-ca');
似乎locale被加载到当前选定的locale中,覆盖了之前设置的任何locale信息。所以先切换语言环境,再加载语言环境信息不会导致这个问题。
我还必须导入语言:
import moment from 'moment'
import 'moment/locale/es' // without this line it didn't work
moment.locale('es')
然后像平常一样利用这一刻
console.log(moment(date).fromNow())