简体   繁体   English

具有自定义月份名称的 DateFormat

[英]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:预期结果:

  • For chosen US locale: AAA 1st, 2022对于选定的US地区: AAA 1st, 2022
  • For chosen DE locale: 1. AAA 2022对于所选的DE语言环境: 1. AAA 2022
  • of course, every other locale format must be supported...当然,必须支持所有其他语言环境格式......

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]);
  1. First create the normal date string to receive the locale's date format.首先创建正常的日期字符串以接收语言环境的日期格式。
  2. Calculate another date string which only includes the locale's month name.计算另一个仅包含语言环境月份名称的日期字符串。 Now I know the string which I need to replace in dateStr no matter which language I choose.现在我知道我需要在dateStr中替换的字符串,无论我选择哪种语言。
  3. Replace the month name with my custom month name.将月份名称替换为我的自定义月份名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM