根据moment.js documentation,你可以使用除全局设置之外的其他语言环境创建一个本地时刻实例来格式化日期。
使用format()工作正常,但是如何在本地实例上使用duration()?
以下是我的尝试:
moment.locale('en');
var localMoment = moment("2015-03-12");
localMoment.locale('de');
// format date:
moment("2015-03-12").format("LL"); // "March 12, 2015"
localMoment.format("LL"); // "12. März 2015"
// format duration:
moment.duration(3600000).humanize(); // "an hour"
localMoment.duration(3600000).humanize(); // TypeError: localMoment.duration is not a function
答案 0 :(得分:4)
简而言之,您无法使用localMoment
。
被叫moment()
返回的时刻对象没有duration
函数(无论您是设置特定区域设置还是保留默认值)。
duration
功能属于模块本身(require
调用返回的'对象)。它显然是这样设计的。 documentation表示持续时间是无环境的,因此没有明确的时刻。
要获得你想要的东西,你需要做这样的事情:
moment.duration( 3600000 ).locale('de').humanize(); //eine Stunde
区域设置仅适用于该调用:
moment.duration( 3600000 ).humanize(); //an hour
moment.duration( 3600000 ).locale('de').humanize(); //eine Stunde
moment.duration( 3600000 ).humanize(); //an hour