[英]DateFormat with custom month names
I have a list of 12 month names, let's say ['AAA', 'BBB', ...]
.我有一个 12 个月名称的列表,比方说
['AAA', 'BBB', ...]
。 In reality, they are kind of historical, so not included in any typical locale.实际上,它们有点历史意义,因此不包括在任何典型的语言环境中。
Now I want them to be included into my DateFormat('yMMMMd')
instead of the normal names.现在我希望将它们包含在我的
DateFormat('yMMMMd')
中而不是普通名称中。 I am not able to build the date string manually like我无法像手动构建日期字符串
var myDateStr = myMonthName + ' ' + myDay + ', ' + myYear; // US
var myDateStr = myDay + '. ' + myMonthName + ' ' + myYear; // DE
because I want to keep the format of the chosen locale n.netheless.因为我想保留所选语言环境的格式n.netheless。 The day/month/year order changes with the chosen locale, which I want to keep.
日/月/年顺序随着我想保留的所选语言环境而变化。 I just want to replace the month name.
我只想替换月份名称。 A RegExp replace or similar is not easily possible, I guess, because of the different date formats and language specific month names.
我猜,由于日期格式和语言特定的月份名称不同,RegExp 替换或类似内容并不容易实现。
Expected result:预期结果:
US
locale: AAA 1st, 2022
US
地区: AAA 1st, 2022
DE
locale: 1. AAA 2022
DE
语言环境: 1. AAA 2022
Do you have any ideas?你有什么想法?
Is it possible to just insert custom month names into DateFormat object?是否可以将自定义月份名称插入 DateFormat object?
I recently had an idea, which seems to work:我最近有一个想法,似乎可行:
var dateStr = DateFormat('yMMMMd', myLocale).format(myDate);
var monthName = DateFormat('MMMM', myLocale).format(myDate);
return dateStr.replaceFirst(monthName, 'AAA');
// or
return dateStr.replaceFirst(monthName, myHistoricalNames[myDate.month - 1]);
dateStr
no matter which language I choose.dateStr
中替换的字符串,无论我选择哪种语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.