Skip to content
Advertisement

.net core date format on Linux

I’m migrating my code from .net framework to .net core and testing it on a Linux machine, one of the issues I had a problem with is the date format when calling DateTime ToString() function, in Windows the default date format is guided by the OS and can be customized in the settings I’ve tried to do something similar in Linux by defining a new locale file with the wanted format and setting up the proper variable (LC_Time). When asking the system for the date I get an answer in the wanted format but when running the code I still get “wrong” one. Am I missing something?

Advertisement

Answer

Old question, but I just had that issue. It’s a “bug / feature / expected behavior” of .NET Core 2…

This is happening because the default short date for en-US (on Linux) is M/d/yy. the way determine the default short date is the code

https://github.com/dotnet/coreclr/blob/master/src/corefx/System.Globalization.Native/calendarData.cpp#L527

https://github.com/dotnet/coreclr/blob/master/src/corefx/System.Globalization.Native/calendarData.cpp#L281

the default short date pattern used is the first one returned from this enumeration and it looks that what ICU return for KShort

But, it will change/be fixed in .NET Core 3 according to this pull request:

I tried to use dotnet core build pack 2.1.3, it did not resolve my issue (still getting 7/6/18) for format d, could you please help me to resolve for the same. Thanks in advance

This is fixed in 3.0. we didn’t change 2.1 behavior for app compatibility reason.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement