3

I have issue with formatting date to dd MMM yyyy. when i try to format this date with Swedish culture I get date as 21 apr. 2023 where it should be 21 apr 2023 I tried below code in console app with .net core 7 and its same:

var date = DateTime.UtcNow;
var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(c => c.LCID == 29);
Console.WriteLine(cultureInfo.LCID);
var dateString = date.ToString("dd MMM yyyy", cultureInfo);
Console.WriteLine(dateString);

Please note this works when i run this program in Azure app service. so there must be something with my windows 11 enterprise 21H2 operating system or I am not sure where is the issue? Any help would be appreciated

I tried

var date = DateTime.UtcNow;
var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(c => c.LCID == 29);
Console.WriteLine(cultureInfo.LCID);
var dateString = date.ToString("dd MMM yyyy", cultureInfo);
Console.WriteLine(dateString);

I expect it to return 21 apr 2023

Son of Man
  • 1,213
  • 2
  • 7
  • 27
  • you could add a `var dateString = date.ToString("dd MMM yyyy", cultureInfo).Replace(".","");` this would remove the unwanted dot if it was present and keep things unchanged if it came up – Denis Schaf Apr 21 '23 at 06:09
  • @DenisSchaf That would be a workaround at best. Swapnil: What are your Date Format settings in your Win 11 System? – Fildor Apr 21 '23 at 06:11
  • Thanks No that wont work because for march in swedish it will be mars not mar. so that will stay 4 characters but I am more looking for why its broken because it was certainly working before. I think its after i moved from .net framework to .net core but I am not sure. – Swapnil Lomate Apr 21 '23 at 06:12
  • 1
    agree, thats why its a comment not an answer. – Denis Schaf Apr 21 '23 at 06:12
  • When i run these commands in poswershell I get : PS C:\Users\swalo> $C=Get-Culture PS C:\Users\swalo> $C.DateTimeFormat FullDateTimePattern : dddd, MMMM d, yyyy h:mm:ss tt LongDatePattern : dddd, MMMM d, yyyy LongTimePattern : h:mm:ss tt MonthDayPattern : MMMM d AbbreviatedMonthNames : {Jan, Feb, Mar, Apr...} MonthNames : {January, February, March, April...} AbbreviatedMonthGenitiveNames : {Jan, Feb, Mar, Apr...} MonthGenitiveNames : {January, February, March, April...} – Swapnil Lomate Apr 21 '23 at 06:19
  • @SwapnilLomate, replace the dot with a space – Son of Man Apr 21 '23 at 06:27
  • And this looks ok: PS C:\Users\swalo> $FinalCulture = [Globalization.cultureinfo]::GetCultureInfo("sv-SE") PS C:\Users\swalo> $FinalCulture.DateTimeFormat AMDesignator : AbbreviatedMonthNames : {jan, feb, mar, apr...} MonthNames : {januari, februari, mars, april...} IsReadOnly : True NativeCalendarName : Gregoriansk kalender AbbreviatedMonthGenitiveNames : {jan, feb, mar, apr...} MonthGenitiveNames : {januari, februari, mars, april...} – Swapnil Lomate Apr 21 '23 at 06:39
  • @SwapnilLomate You want an answer that will return 21 Mar 2023 and not 21 mars 2023? – Son of Man Apr 21 '23 at 06:46
  • yes because i said MMM – Swapnil Lomate Apr 21 '23 at 09:50
  • 1
    @Fildor it seems to be another ICU vs NLS issue. – Guru Stron Apr 21 '23 at 12:39

1 Answers1

1

Check out the .NET globalization and ICU doc:

Before .NET 5, the .NET globalization APIs used different underlying libraries on different platforms. On Unix, the APIs used International Components for Unicode (ICU), and on Windows, they used National Language Support (NLS). This resulted in some behavioral differences in a handful of globalization APIs when running applications on different platforms. Behavior differences were evident in these areas:

  • Cultures and culture data
  • ...

If you switch back to NLS you should get the expected output (at least works on "my machine"). For example by adding the following to .csproj:

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

You should get expected "21 apr 2023".

See also - Behavior changes when comparing strings on .NET 5+.

P.S.

If I understand correctly - this line is resposnible.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132