DateTime到字符串格式返回错误结果

发布于 2025-02-10 19:04:01 字数 1490 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2025-02-17 19:04:01

tl; dr;

使用不变文化,如果您总是想获得jun

((DateTime)somedate).ToString("ddd MMM dd yyyy", CultureInfo.InvariantCulture)

更长的解释

真的很有趣,为什么(有时)发生这种情况

您应该检查您使用的文化的缩写的monthgenitivenames

重现它,

您可以重现行为,当您使用具有缩写的月份名称的文化时,与默认值不同:

var culture = new CultureInfo("en-US");
culture.DateTimeFormat.AbbreviatedMonthNames = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "" };
culture.DateTimeFormat.AbbreviatedMonthGenitiveNames = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "" };

Console.WriteLine(new DateTime(2022, 6, 21).ToString("ddd MMM dd yyyy", culture)); // Tue June 21 2022

解释

有一个解释abbreviatedMonthgenitivenames的文档中

在某些语言中,一个月的名称是日期的一部分
属性病例。例如,在Ru-Ru或俄罗斯(俄罗斯)的日期
文化由日常数字和属格月份的名称组成
为1月1月1日(1月1日)。对于这些文化,如果自定义格式字符串
包括“ MMM”格式指定符,dateTime.toStringtoString
方法包括适当的成员
缩写的monthgenitivenames阵列代替了“ mmm”
结果字符串

也有一个问题也提到了这一点:

这意味着在格式化包含一天的日期时(即
在日期模式中使用ddd),然后属格名称为
用过的。当您使用日期模式d.mmm.yyyy时,这意味着
日期将使用Genitive的月份名称进行格式。你会得到一个
格式化日期类似1.Jan..2020。注意属格月份
这里的名称是jan。,而不是JAN。如果解析格式的字符串
1.Jan..2020这将被罚款。您不是在解析格式的字符串,而是在解析构造的字符串
1.Jan.2020不是正确的表格。如果要使用De-De获得想要的结果,则可以使用d.mmyyyy之类的东西或您
格式带有日名代替日常号码,例如ddd.mmm.m.yyyy

可能的解决方案

您可以使用不变文化来获取您要寻找的结果:

new DateTime(2022, 6, 21).ToString("ddd MMM dd yyyy", CultureInfo.InvariantCulture)

此结果:tue> tue> tue>

TL;DR;

Use the invariant culture, if you always want to get Jun:

((DateTime)somedate).ToString("ddd MMM dd yyyy", CultureInfo.InvariantCulture)

Longer explanation

That's really interesting why this happens (sometimes).

You should check the values of your AbbreviatedMonthGenitiveNames of the culture you're using.

Reproduce it

You can reproduce the behavior, when you use a culture with abbreviated month genitive names that differ from the default:

var culture = new CultureInfo("en-US");
culture.DateTimeFormat.AbbreviatedMonthNames = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "" };
culture.DateTimeFormat.AbbreviatedMonthGenitiveNames = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "" };

Console.WriteLine(new DateTime(2022, 6, 21).ToString("ddd MMM dd yyyy", culture)); // Tue June 21 2022

Explanations

There is an explanation in the documentation of AbbreviatedMonthGenitiveNames:

In some languages, a month name that is part of a date appears in the
genitive case. For example, a date in the ru-RU or Russian (Russia)
culture consists of the day number and the genitive month name, such
as 1 Января (1 January). For these cultures, if a custom format string
includes the "MMM" format specifier, the DateTime.ToString or ToString
method includes the appropriate member of the
AbbreviatedMonthGenitiveNames array in place of the "MMM" in the
result string
.

and there is an issue where this is mentioned too:

This means when formatting a date containing the day as a number (i.e.
using d or dd in the date pattern), then the genitive names will be
used. As you are using the date pattern d.MMM.yyyy, this means the
date will be formatted using the genitive month names. You will get a
formatted date something like 1.Jan..2020. Note the genitive month
name here is Jan. and not Jan. If you parse back the formatted string
1.Jan..2020 this will be parsed fine. You are not parsing the formatted string instead you are parsing your constructed string
1.Jan.2020 which is not a correct form. If you want to get the result you want with de-DE, you may use something like d.MMMyyyy or you
format with day names instead of day number, like ddd.MMM.yyyy

Possible solution

You could use the invariant culture to get the result you are looking for:

new DateTime(2022, 6, 21).ToString("ddd MMM dd yyyy", CultureInfo.InvariantCulture)

This results in: Tue Jun 21 2022

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文