asp.net - 不同机器上相同文化的不同 DateTimeFormat

标签 asp.net datetime cultureinfo

在不同的服务器上部署 Web 应用程序时遇到问题。似乎有些不一致 DateTimeFormat模式,如 ShortDatePattern , 使用相同的文化 (pt-BR)。

在我的开发机器(安装了 Windows 7,.NET 4,应用程序面向 .NET 3.5)和 Windows Server 2008 R2(应用程序面向 .NET 4)服务器中,ShortDatePattern 是 "dd/MM/yyyy" - 这是正确的,我猜。

在生产服务器(Windows Server 2003,使用 .NET 3.5)中,它是 "d/M/yyyy" .它给我带来了很多麻烦。

我可以通过手动设置模式来解决这个问题,但我真的很想避免每次需要输出日期时都这样做。特别是因为这在许多地方(比如我使用 MVC 的 Html.TextBoxFor 的地方)都不是微不足道的,并且需要大量的重写。

如果有一种方法可以在一个地方更改整个 Web 应用程序的模式,那就太好了。我在 Global.asax.cs 中尝试了以下方法文件,没有成功:

CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;

有任何想法吗?

谢谢你。

最佳答案

实际上,我试图在 Global.asax 文件中的错误位置使用上面的代码。

通过将代码放入 Application_BeginRequest 方法,我设法覆盖了整个应用程序的 ShortDatePattern:

protected void Application_BeginRequest()
{
    CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
    info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    System.Threading.Thread.CurrentThread.CurrentCulture = info;
}

关于asp.net - 不同机器上相同文化的不同 DateTimeFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5505647/

相关文章:

asp.net - 自定义 ASP.NET 通用提供程序

javascript - 在 asp.net 中服务器端验证后可以运行自定义代码吗?

ruby-on-rails - 将标准 rails/ruby 时间转换为美国格式 - 日期混淆

mysql - 显示 mysql 查询中缺失日期的计数

c# - 创建从左到右与从右到左部分连接的正确路径时出现问题

asp.net-mvc - 我怎样才能通过 LCID 获得文化

c# - 两个 ASP.NET Core 项目之间的依赖注入(inject)

asp.net - 工作线程和 I/O 线程有什么区别?

datetime - Dart -两个不同的日期在天中产生相同的差异

c# - 文化不变的 Decimal.TryParse()