【问题标题】:DateTime formatting doesn't match the cultureDateTime 格式与文化不匹配
【发布时间】:2015-01-30 18:20:16
【问题描述】:

我有一个 Windows Server 2008 R2 系统,它配置了默认的语言环境设置(不覆盖格式或任何内容),并且设置为 en-US。

当我询问以下内容时: System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat

它将 DateSeparator 列为 -(破折号)而不是 /(斜杠)。所以 DateTime.Now.ToString() 看起来像:

2015 年 1 月 30 日

另外,CurrentThread.CurrentCulture.ToString() = "en-US"

我完全不知道这怎么可能,但更重要的是,我想知道 .NET 是否有某种可以以某种方式配置的语言环境覆盖?

系统区域和语言设置正常并且没有更改。任何帮助将不胜感激,谢谢。

这是诊断信息的代码。它在 aspx 页面上的 asp.net 中运行。

Current Date Time: <%= DateTime.Now.ToString() %>
Current Short Date: <%= DateTime.Now.ToShortDateString() %>
Current Culture: <%= System.Threading.Thread.CurrentThread.CurrentCulture.ToString() %>
Current UI Culture: <%= System.Threading.Thread.CurrentUICulture.ToString() %>

DateTimeFormatInfo invariant = CultureInfo.InvariantCulture.DateTimeForamat;
DateTimeFormatInfo uiThread = System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat;
DateTimeFormatInfo thread = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;

Type type = System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.GetType();
foreach( var prop in type.GetProperties()) {
  <%= prop.Name %> 
  <%= prop.GetValue(invariant, null) %> <br/>
  <%= prop.GetValue(uiThread, null) %> <br/>
  <%= prop.GetValue(thread, null) %> <br/>
}

地区和语言设置

这里有一些附加信息

【问题讨论】:

  • 所以大概你可以在一个小小的控制台应用程序中看到这个?演示会非常方便,只是为了确保我们都在考虑相同的事情。
  • 请提供您的图片示例的代码。你的地区和语言设置看起来像The Default(短日期是格式 M/d/yyyy)?
  • 这不是美国机器上的默认设置。
  • 检查用于运行 ASP.NET 应用程序的应用程序池所用帐户的区域设置。它们可能与您在“区域和语言设置”对话框中查看的设置不同。
  • 当您尝试使用 System.Globalization.CultureInfo.GetCultureInfo("en-US") 静态(即不是从系统设置)获取它们时会发生什么?

标签: c# asp.net datetime locale


【解决方案1】:

每个用户都有不同的地区资料。您当前的帐户和应用程序可能没有使用相同的帐户,这导致您看到不同的值。

转到区域设置窗口 -> 管理 -> 单击复制设置并勾选所有复选框(欢迎屏幕和系统帐户 + 新用户帐户),然后单击确定。看看能不能解决你的问题。如果您的应用是 Web 应用程序,您需要创建另一个 AppPool 才能看到它受到影响。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多