c# - Linux 上的 .net 核心日期格式

标签 c# .net linux .net-core

我正在将我的代码从 .net 框架迁移到 .net 核心并在 Linux 机器上测试它,我遇到的问题之一是调用 DateTime ToString() 函数时的日期格式,在 Windows 中是默认格式日期格式由操作系统引导,可以在设置中自定义我试图通过定义具有所需格式的新语言环境文件并设置适当的变量(LC_Time)来在 Linux 中做类似的事情。当向系统询问日期时,我得到了所需格式的答案,但在运行代码时,我仍然得到“错误”的答案。我错过了什么吗?

最佳答案

老问题,但我刚遇到这个问题。这是一个"bug / feature / expected behavior" .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

但是,它会根据这个在 .NET Core 3 中更改/修复 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

这是在 3.0 中修复的。出于应用兼容性原因,我们没有更改 2.1 行为。

关于c# - Linux 上的 .net 核心日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52524957/

相关文章:

linux - 将端口 80 上的所有数据包从虚拟机内部路由到另一个虚拟机上的 kali linux

windows - linux下maven-jetty-plugin无法绑定(bind)地址

c# - 如何正确和完全关闭/重置 TcpClient 连接?

c# - ControlDesigner 类 (C# WinForms)

c# - 同步多个 View 、编辑器和提交/取消

.net - 如何阻止异常警报发狂

c# - 整个解决方案的Visual Studio快速切换平台

linux - 在无密码 ssh 登录后检查远程用户的身份?

c# - 使用表达式为 Entity Framework 构建 Array.Contains

c# - Unity - 为同一个接口(interface)注入(inject)不同的类