【问题标题】:moment locale is not setting the locale locally时刻语言环境没有在本地设置语言环境
【发布时间】:2020-10-19 12:25:32
【问题描述】:

我有各种文化的日期(例如 19.10.2020(德语)2020/10/19(英语))所以现在在将数据保存到数据库时,我必须将所有格式恢复为基本的美国文化(YYYY- MM-DD)。为此我做了以下-

//// this method will convert the date of any locales (e.g. fr, de, ja, sv, nb) to en-US locale to save the date in DB
  reverseDateToEnLocale(date: string, format?: string) {
    var locale = moment();
    locale.locale(this.locale); //// this.locale is the set locale fr, zh, de, en etc.
    if (!format) { format = "YYYY-MM-DD"; } //// the format I want to convert for saving in database
    var localeFormat = moment.localeData().longDateFormat('L'); ///// to get the set moment locale date format
    return moment(date, localeFormat).format(format); /// convert the format to expected en
  }

但是当我进入时


    var locale = moment();
    locale.locale(this.locale); 
    console.log(moment().locale()); // everytime reverts me the en 

我不想更改全局时刻区域设置。我只是希望它仅在函数中使用以恢复并获得所需的结果,即任何语言环境日期格式的英文日期格式。

这段代码在德语格式时可以正常工作,但对于中文格式则不起作用。我不明白遗漏了什么。

when I pass 'zh' as locale then locale in moment is not getting set. And returning me the wrong format

https://momentjs.com/docs/#/i18n/我也尝试过使用下面的设置

moment.locale('en'); // default the locale to English
var localLocale = moment();

localLocale.locale('fr'); // set this instance to use French
localLocale.format('LLLL'); // dimanche 15 juillet 2012 11:01
moment().format('LLLL'); // Sunday, July 15 2012 11:01 AM

但它不起作用 请你告诉我出了什么问题。

【问题讨论】:

    标签: angular typescript internationalization momentjs


    【解决方案1】:

    嗯,我知道有一些适用和理解的时刻语言环境列表。我传递的语言环境是'zh-Hant',暂时不接受。 Moment 有自己的语言环境列表。请在此处查看-

    How to obtain a list of available locale keys from momentjs with locale?

    因此,我花了很长时间才弄清楚这一点。如果我们将时刻用于日期和数字格式,则仅使用并且仅被他们接受的语言环境。否则,如果我们传递不同的语言环境,它可能会产生问题并且它会中断。

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 2013-01-10
      • 2020-04-05
      相关资源
      最近更新 更多