Reputation: 320
I have this simple code sample in 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. It returns a filename 'CustomerX-Invoice-1100-26-aug-2022'.
After migrating to .NET6 it's suddenly adding a dot to the abbreviation of the month. It's now returning '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. 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? When I for example change it to 'en-US' no dot is added to the month abbreviation.
Linqpad sample using .NET Core 3.1:
Linqpad sample using .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.
Any suggestions on what's happening here and how handle this the best way?
Upvotes: 5
Views: 1594
Reputation: 182
This is due to the rules of the nl-NL culture format. 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. 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);
Upvotes: 4