【问题标题】:Moment: format using other locale without changing global moment localeMoment:使用其他语言环境格式化而不更改全局时刻语言环境
【发布时间】:2017-04-10 07:57:17
【问题描述】:

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

我知道我可以这样做:

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

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

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

【问题讨论】:

    标签: javascript internationalization momentjs i18next


    【解决方案1】:

    来自moment docs - Changing locales locally

    var aa = moment();
    
    aa.locale('fr');
    aa.format('LLLL');
    
    aa.locale('en');
    aa.format('LLLL');
    

    【讨论】:

      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 2017-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多