2017-04-10 108 views
0

我有一個要求,即使moment.locale()設置爲其他語言,我需要在給定語言環境下格式化某些特定時刻對象。是否有可能以某種方式調用format,使得它僅對當前操作使用靜態語言環境?矩:使用其他語言環境而不改變全局時間語言環境的格式

我知道我可以這樣做:

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

然而,這種感覺完全錯誤的。我想反而是一個類似於:

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

回答

1

設置全局區域設置:

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