DateTimeOffset.TryParse 不在 .Net 5.0 中格式化俄语 RFC 日期,但它在 .Net 3.1 中有效。为什么?
DateTimeOffset.TryParse doesn't format a russian RFC Date in .Net 5.0 but it works in .Net 3.1. Why?
这是我的代码:
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());
}
}
日期字符串:
"Ср, 17 фев 2021 15:03:25 +0300"
.Net 5.0 输出:
.Net 3.1 输出:
我需要 3.1 的输出,但在 5.0 中,我已经尝试了一些无效的解决方案,但我不确定为什么。当我调试它时,它说日期不是正确的日期格式,但为什么它在 3.1 而不是 5.0 中工作?
感谢@HansPassant GitHub Link。
我设法找到了解决问题的办法
从这个 link 我能够从 Microsoft docs.
中找到一些信息
我将下面的代码添加到 .csproj 文件并且它有效。
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Globalization.UseNls"Value="true" />
</ItemGroup>
这是我的代码:
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());
}
}
日期字符串: "Ср, 17 фев 2021 15:03:25 +0300"
.Net 5.0 输出:
.Net 3.1 输出:
我需要 3.1 的输出,但在 5.0 中,我已经尝试了一些无效的解决方案,但我不确定为什么。当我调试它时,它说日期不是正确的日期格式,但为什么它在 3.1 而不是 5.0 中工作?
感谢@HansPassant GitHub Link。
我设法找到了解决问题的办法从这个 link 我能够从 Microsoft docs.
中找到一些信息我将下面的代码添加到 .csproj 文件并且它有效。
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Globalization.UseNls"Value="true" />
</ItemGroup>