Moment:使用其他语言环境格式化而不改变全局时刻语言环境
Moment: format using other locale without changing global moment locale
我有一个要求,我需要在给定的语言环境下格式化一些特定的时刻对象,即使 moment.locale()
设置为其他内容。是否有可能以某种方式调用 format
,使其仅对当前操作使用静态语言环境?
我知道我可以做类似的事情:
let oldLocale = moment.locale();
moment.locale('theStaticLocale');
let formattedDate = moment.format('asdasd');
moment.locale(oldLocale);
然而,这感觉完全不对。我想要的是类似于:
let formattedDate = moment.format('asdasd','theStaticLocale');
正在设置全局语言环境:
moment.locale('en');
设置将使用全局语言环境的 moment 对象:
let g = moment();
设置将使用其他语言环境的 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
来自 moment docs - Changing locales locally
var aa = moment();
aa.locale('fr');
aa.format('LLLL');
aa.locale('en');
aa.format('LLLL');
我有一个要求,我需要在给定的语言环境下格式化一些特定的时刻对象,即使 moment.locale()
设置为其他内容。是否有可能以某种方式调用 format
,使其仅对当前操作使用静态语言环境?
我知道我可以做类似的事情:
let oldLocale = moment.locale();
moment.locale('theStaticLocale');
let formattedDate = moment.format('asdasd');
moment.locale(oldLocale);
然而,这感觉完全不对。我想要的是类似于:
let formattedDate = moment.format('asdasd','theStaticLocale');
正在设置全局语言环境:
moment.locale('en');
设置将使用全局语言环境的 moment 对象:
let g = moment();
设置将使用其他语言环境的 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
来自 moment docs - Changing locales locally
var aa = moment();
aa.locale('fr');
aa.format('LLLL');
aa.locale('en');
aa.format('LLLL');