如果 de-DE 语言环境包含其他选项,则使用 .toLocaleDateString() 不能正确缩短"{month: 'short'}"月份



德语中的缩短值在只有"月份;选项在选项中传递。还试图格式化";"天";改变了";月份;值返回。此问题在en-US区域设置中不存在,但在de-DE中存在。

我并没有要求解决这种行为——手动格式化字符串、覆盖月份或使用日期时间库都是可能的解决方案,而且都很容易实现。我很感兴趣为什么会发生这种情况。

这种行为在Chrome、Firefox和Node中始终发生。

将月份更改为一月(德语:Januar(可以正确地将月份缩短为三个字符:在两种语言环境中都是Jan。将具有4个字母名称的月份(5月、6月、7月(传递给onlyMonthOption将返回3个字符的字符串,而将它们传递给buggedOption将返回完整的4个字符字符串,而预期会返回3个字符串。

我认为month: 'short'不应该返回两个不同的值,这取决于是否包括其他选项。这似乎是一个可能的实现错误。

const today = new Date(Date.UTC(2021, 5, 2, 0, 0, 0));
const onlyMonthOption = { month: 'short' };
const buggedOption = { month: 'short', day: 'numeric' };
//de-DE behaves unexpectedly
console.log(today.toLocaleDateString('de-DE', onlyMonthOption)); // Properly returns "Jun"
console.log(today.toLocaleDateString('de-DE', buggedOption)) // Improperly returns "Juni", expected "Jun"
//en-US works as expected
console.log(today.toLocaleDateString('en-US', onlyMonthOption)); // Properly returns "Jun"
console.log(today.toLocaleDateString('en-US', buggedOption)) // Propery returns "Jun"

德国人不要用4个或更少的字母缩写月份。他们可能会觉得它们足够短,并试图避免歧义。

https://web.library.yale.edu/cataloging/months

还要注意的是,九月不像许多其他语言中的sep

还请注意,你确实有一点,因为根据上面的链接,丹麦语(我的母语(也不缩写四个或更少,但在JS中他们做

因此,我的解释是,仅月份就有3个字符,而日期则有4个完整或更多的缩写,即4个字母(包括句号(。如果一个月有四个或更少的字母,那么就没有句号。

德语一月/Jän.二月März四月Mai Juni Juli八月Sept.Okt.十一月Dez
丹麦语一月至二月。四月集市六月集市七月八月九月十月十一月十二月

const shortMonth = {
month: 'short',
};
const shortMonthDay = {
month: 'short',
day: 'numeric'
};
for (let i = 0; i < 12; i++) {
const today = new Date(Date.UTC(2021, i, 2, 0, 0, 0));
console.log("German month",today.toLocaleDateString('de-DE', shortMonth))
console.log("German month day",today.toLocaleDateString('de-DE', shortMonthDay))
console.log("Danish month",today.toLocaleDateString('da-DK', shortMonth))
console.log("Danish month day",today.toLocaleDateString('da-DK', shortMonthDay))
}

相关内容

最新更新