I have a requirement where I need to format some specific moment objects under a given locale, even if moment.locale()
is set to something else. Is it possible to somehow invoke format
in such a way that it uses the static locale only for the current operation?
I know that I can do something like:
let oldLocale = moment.locale();
moment.locale('theStaticLocale');
let formattedDate = moment.format('asdasd');
moment.locale(oldLocale);
However, this feels completely wrong. What I want instead is something akin to:
let formattedDate = moment.format('asdasd','theStaticLocale');
From moment docs - Changing locales locally
var aa = moment();
aa.locale('fr');
aa.format('LLLL');
aa.locale('en');
aa.format('LLLL');
Setting global locale:
moment.locale('en');
Setting a moment object that’ll use global locale:
let g = moment();
Setting a moment object that’ll use another locale:
let x = moment();
x.locale('fr');
Printing:
console.log(g.format('LLLL')); // Sunday, July 15 2012 11:01 AM
console.log(x.format('LLLL')); // dimanche 15 juillet 2012 11:01
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With