时刻:使用其他语言环境格式而不更改全局时刻区域设置

时间:2017-04-10 07:57:17

标签: javascript internationalization momentjs i18next

我有一个要求,我需要格式化给定语言环境下的某些特定时刻对象,即使moment.locale()设置为其他内容。是否有可能以某种方式调用format,使其仅将静态语言环境用于当前操作?

我知道我可以做类似的事情:

let oldLocale = moment.locale();
moment.locale('theStaticLocale');
let formattedDate = moment.format('asdasd');
moment.locale(oldLocale);

但是,这感觉完全错了。我想要的是类似于:

let formattedDate = moment.format('asdasd','theStaticLocale');

2 个答案:

答案 0 :(得分:2)

设置全局区域设置:

moment.locale('en');

设置将使用全局区域设置的时刻对象:

let g = moment();

设置将使用其他语言环境的时刻对象:

let x = moment();
x.locale('fr');

印刷:

console.log(g.format('LLLL')); // Sunday, July 15 2012 11:01 AM
console.log(x.format('LLLL')); // dimanche 15 juillet 2012 11:01

答案 1 :(得分:2)

来自moment docs - Changing locales locally

var aa = moment();

aa.locale('fr');
aa.format('LLLL');

aa.locale('en');
aa.format('LLLL');
相关问题