Search code examples
c#asp.net-core.net-8.0globalization.net-4.8

Date format culture differences with .NET 4.8 and .NET 8


I have this bit of code and when running in .NET 4.8 gives a different result than .NET 8

 var cultureInfo = CultureInfo.CreateSpecificCulture("ar-KW");
 var date = new DateTime(2024, 10, 12);
 string result = date.ToString("d", cultureInfo);

.NET 4.8 => 12/10/2024

.NET 8.0 => 12‏‏/10‏‏/2024

The .NET 8 version does appear to have special characters (unicode??).

Can someone explain the differences and how to get the .NET 8 to perform consistently with 4.8.


Solution

  • This is due to changes made to .NET to support ICU rather than NLS

    If you want to revert to the previous behaviour in a .NET 8 application, add the following to the ".csproj" project file and rebuild:

    <ItemGroup>
      <RuntimeHostConfigurationOption Include="System.Globalization.UseNls" Value="true" />
    </ItemGroup>
    

    The odd unicode characters you are seeing in the date output are the "right-to-left" mark.

    A Console App doesn't seem to be able to display this correctly (even if you add Console.OutputEncoding = System.Text.Encoding.Unicode).

    However, if you output this in a Windows message box it is correctly displayed:

    Correctly displayed Arabic date