当语言环境未在本地设置语言环境

时间:2020-10-19 12:25:32

标签: angular typescript internationalization momentjs

我在各种文化中都有约会(例如,2020年10月19日(德语)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

但是它不起作用 请您告诉我哪里出了问题。

1 个答案:

答案 0 :(得分:0)

好吧,我知道有一些适用于瞬间的,可以理解的瞬间语言环境列表。我传递的localeId是'zh-Hant',目前还不接受。 Moment有自己的语言环境列表。请在这里检查-

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

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