Search code examples
c#datetime-formatdatetimeoffset

DateTimeOffset.TryParse doesn't format a russian RFC Date in .Net 5.0 but it works in .Net 3.1. Why?


This is my code:

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        CultureInfo cultureInfo = new CultureInfo("ru");
        DateTimeOffset dt;
        bool parseSuccess = DateTimeOffset.TryParse("Ср, 17 фев 2021 15:03:25 +0300", 
            cultureInfo.DateTimeFormat, DateTimeStyles.None, out dt);
        Console.WriteLine(dt.ToUniversalTime());
    }
}

Date String: "Ср, 17 фев 2021 15:03:25 +0300"

.Net 5.0 Output: enter image description here

.Net 3.1 Output: enter image description here

I need the output from 3.1 but in 5.0 and I've tried quiet a few solutions which didn't work and I'm not sure why. When I debug it, it says that the date is not a correct date format but then why does it work in 3.1 and not 5.0?


Solution

  • I managed to find a solution to my issue thanks to @HansPassant GitHub Link.

    From this link I was able to find some information from the Microsoft docs.

    I added the code below to the .csproj file and it works.

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