DateTime.TryParse has different behavior on Linux and Windows

  .net-core, c++, docker, linux, windows

I try to understand how DateTime.TryParse works on different environments in .NET Core

Let’s say that I call two methods

DateTime.TryParse("08-04-2020", new CultureInfo("da-DK"), DateTimeStyles.None, out var res)
DateTime.TryParse("08/04/2020", new CultureInfo("da-DK"), DateTimeStyles.None, out var res)

When I run it on Linux (docker) it returns false for first method and true for the second one.
When I run it on Windows it returns true for both methods.

I checked that on Linux default value for CultureInfo.DateTimeFormat.DateSeparator in da-DK culture is "/" and probably that’s why it returns true only for the second method. But on Windows default value for this separator is "-" so the second method should return false. Why does it return true? it looks like the date separator is taken into account only on Linux env.

Source: Windows Questions

LEAVE A COMMENT