Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#DateTime.ToString()在Linux Ubuntu 18.04上是错误的,尽管在当前线程中设置了正确的区域设置_C#_Linux_Datetime_.net Core_Ubuntu 18.04 - Fatal编程技术网

C#DateTime.ToString()在Linux Ubuntu 18.04上是错误的,尽管在当前线程中设置了正确的区域设置

C#DateTime.ToString()在Linux Ubuntu 18.04上是错误的,尽管在当前线程中设置了正确的区域设置,c#,linux,datetime,.net-core,ubuntu-18.04,C#,Linux,Datetime,.net Core,Ubuntu 18.04,好吧,这让我快发疯了。我安装了一个新的ubuntu 18.04服务器实例,它运行我的dotnetcore 2.2 mvc应用程序 尽管在操作系统和当前线程中似乎正确设置了区域设置,但DateTime.ToString()函数为我提供了一种wierd数据格式: 格式错误:19-05-09 12 21 02 2 预期格式:19-05-09 12:21:01 在这两种情况下,当前线程本地都是“fr CA”。 在操作系统中,“locale”命令在这两种情况下都返回“en_US” 我已经检查了Curr

好吧,这让我快发疯了。我安装了一个新的ubuntu 18.04服务器实例,它运行我的dotnetcore 2.2 mvc应用程序

尽管在操作系统和当前线程中似乎正确设置了区域设置,但DateTime.ToString()函数为我提供了一种wierd数据格式:

  • 格式错误:19-05-09 12 21 02 2
  • 预期格式:19-05-09 12:21:01
在这两种情况下,当前线程本地都是“fr CA”。 在操作系统中,“locale”命令在这两种情况下都返回“en_US”

我已经检查了CurrentThread.CurrentCulture.DateTimeFormat中的depper,但在这两种情况下它似乎并不相同。查看CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern变量时:

  • 错误的格式输出模式给我:HH mm
  • 好格式的服务器输出模式给了我:HH:mm

这是什么魔法?我的鼻子在流血。

就像@Powerlord在评论中说的那样,你为什么不指定约会的格式呢

大概是这样的:

DateTime.ToString("dd-MM-yy HH:mm:ss");
输出:

09-05-19 17:43:10

在查看了dotnet核心中DateTime格式的源代码之后,我找到了一个修复方法,以确保ToString()在没有参数的情况下正常工作

System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.LongTimePattern = "HH:mm:ss";
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";            
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.TimeSeparator = ":";

System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat = System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat;

我可以复制您的结果,这似乎是.NETCore如何解释这种特定文化的CLDR数据的一个特定错误


我在这里报告了此问题:

此问题的修复程序将在.NET Core 3.0中

如果要解决此问题,而不必每次都接触DateTime.ToString调用,可以尝试执行以下操作:

CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern=“HH'h'mm'min'ss's”;
CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern=“HH'h'mm”//或者选择您喜欢的任何格式

我很惊讶有人使用
.ToString()
而不是
.ToString(string)
,它可以让您指定自定义格式。是的,我知道,但它已经运行了4年,没有任何问题。所以我很惊讶为什么同一个地区会产生不同的结果。因为我需要一个热修复程序,而且我不必花时间在整个应用程序中更改10000个引用。