Search code examples
c#.netglobalization

What "neutral culture" breaks DateTime.TryParse()?


I have the following code snippet

DateTime date1;
CultureInfo neutralCulture = new CultureInfo("fr");
bool isNeutral = neutralCulture.IsNeutralCulture; // True

DateTime.TryParse("not a date", neutralCulture, DateTimeStyles.AdjustToUniversal, out date1);

Which executes without throwing an exception, however, the documentation states

NotSupportedException: provider is a neutral culture and cannot be used in a parsing operation.

"fr" is a neutral culture, as demonstrated by the property on the CultureInfo object above, so I would expect this code to break.

What "neutral culture" breaks this method - is this documented anywhere?


Solution

  • I did a quick test of all the neutral cultures, and not even one threw an exception as shown below:

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
    foreach (var thisCulture in cultures)
    {
        DateTime date1;
        CultureInfo neutralCulture = new CultureInfo(thisCulture.Name);
        bool isNeutral = neutralCulture.IsNeutralCulture; // True
    
        DateTime.TryParse("not a date", neutralCulture, DateTimeStyles.AdjustToUniversal, out date1);
    }
    

    I am not sure what to conclude from that. Could the documentation be wrong? Who knows.