【发布时间】: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