简体   繁体   English

Datetime.ToString 在 .NET6 中为月份缩写添加点

[英]Datetime.ToString adds dot to month abbreviation in .NET6

I have this simple code sample in C#.我在 C# 中有这个简单的代码示例。 It creates a filename voor an invoice:它创建一个文件名或发票:

var x = new DateTime(2022,8,26);
var invoiceNumber = 1100;

var fileName = "CustomerX-Invoice";
    fileName += $"-{invoiceNumber}";
    fileName += $"-{x.ToString("dd-MMM-yyyy", CultureInfo.GetCultureInfo("nl-NL"))}";

Console.WriteLine(fileName);

This was working fine in .NET Core 3.1.这在 .NET Core 3.1 中运行良好。 It returns a filename 'CustomerX-Invoice-1100-26-aug-2022'.它返回一个文件名“CustomerX-Invoice-1100-26-aug-2022”。

After migrating to .NET6 it's suddenly adding a dot to the abbreviation of the month.迁移到 .NET6 后,它突然在月份的缩写中添加了一个点。 It's now returning 'CustomerX-Invoice-1100-26-aug.-2022'.它现在返回“CustomerX-Invoice-1100-26-aug.-2022”。 See the dot after 'aug'.请参阅“八月”之后的点。

When I test it in LINQPad7 I see the problem is introduced in .NET5 already.当我在 LINQPad7 中对其进行测试时,我发现问题已经在 .NET5 中引入。 I can't find out if this is by design or a known bug?我无法确定这是设计使然还是已知错误? It could be an issue with the culture 'nl-NL' also?这也可能是文化“nl-NL”的问题吗? When I for example change it to 'en-US' no dot is added to the month abbreviation.例如,当我将其更改为“en-US”时,月份缩写中不会添加点。

Linqpad sample using .NET Core 3.1:使用 .NET Core 3.1 的 Linqpad 示例: 它在 .NET Core 3.1 中运行良好

Linqpad sample using .NET6:使用 .NET6 的 Linqpad 示例:

在 .NET6 中添加了一个点

Of course I can manipulate the string and remove the dot but I would rather see I can trust .NET to return the month abbreviation without the dot.当然,我可以操纵字符串并删除点,但我宁愿看到我可以信任 .NET 返回没有点的月份缩写。

Any suggestions on what's happening here and how handle this the best way?关于这里发生的事情以及如何以最佳方式处理这个问题的任何建议?

This is due to the rules of the nl-NL culture format.这是由于 nl-NL 文化格式的规则。 You will want to change or remove your culture target or do some basic string manipulation to remove the ".".您将需要更改或删除您的文化目标或进行一些基本的字符串操作以删除“.”。

If this is for internal use in your program and is not shown to the user, you can use CultureInfo.InvariantCulture to get a consistent output every time without any localization.如果这是在您的程序中用于内部使用并且未向用户显示,您可以使用CultureInfo.InvariantCulture每次都获得一致的 output 而无需任何本地化。 I recommend using this Culture for all backend work.我建议将这种文化用于所有后端工作。

var x = new DateTime(2022, 8, 26);
var invoiceNumber = 1100;

var fileName = "CustomerX-Invoice";
fileName += $"-{invoiceNumber}";
fileName += $"-{x.ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture)}";

Console.WriteLine(fileName);

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

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