C# 如何将 asp.net 的 en-US 日期更改为 en-GB?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9700934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:27:37  来源:igfitidea点击:

how to change en-US dates to en-GB for asp.net?

c#asp.netiisdatetime-format

提问by rob

on a developer machine (cassini)

在开发机器上 (cassini)

new DateTime(2012,3,14).ToString("d")

results in

结果是

14/03/2012

which is correct but when deployed to a full IIS server the result is

这是正确的,但是当部署到完整的 IIS 服务器时,结果是

03/14/2012

The server is set in control panel/Region language to all English/UK/GB, running date in command prompt returns the dd/MM/YYYY format.

服务器在控制面板/区域语言中设置为所有英语/英国/GB,命令提示符中的运行日期返回 dd/MM/YYYY 格式。

The site is set for both uiCulture="en-GB"and culture="en-GB"and these show in the web.config globalization tag.

该网站设置为两个uiCulture="en-GB",并culture="en-GB"与这些节目在web.config全球化标签。

I can work around this issue by adding a forced culture

我可以通过添加强制文化来解决这个问题

new DateTime(2012,3,14).ToString("d", new CultureInfo("en-GB"));

but I would really like to know what is setting the format incorrectly.

但我真的很想知道是什么错误地设置了格式。

CultureInfo.CurrentCulture.Name, CultureInfo.CurrentUICulture.Name

both return en-US

都返回 en-US



  • en-US: M/d/yyyy(e.g. 3/14/2012)
  • en-GB: dd/MM/yyyy(e.g. 14/03/2012)
  • en-US: M/d/yyyy(例如 3/14/2012)
  • en-GB: dd/MM/yyyy(例如 14/03/2012)

Actual value in web.config

web.config 中的实际值

 <globalization requestEncoding="UTF-8" responseEncoding="UTF-8" uiCulture="en-GB" culture="en-GB" />

回答by Jon

I managed to get it working by putting this into the web.config

我设法通过将其放入 web.config 使其工作

<globalization culture="en-GB"/>

回答by Richard

In your web.configadd

在您web.config添加

<globalization culture='auto' uiCulture='auto' />

and then, assuming the browser is correctly configured to pass the preferred locale, the worker thread processing the request will have its CurrentCultureand CurrentUICultureset correctly.

然后,假设浏览器正确配置为传递首选语言环境,处理请求的工作线程将正确设置它CurrentCultureCurrentUICulture设置。

Any locale dependent operations (including such things as DateTimeformat d) will use the client's preference.

任何语言环境相关的操作(包括诸如DateTimeformat 之类的东西d)都将使用客户端的首选项。

Globalization element of web.config on MSDN: https://msdn.microsoft.com/en-us/library/ydkak5b9(v=vs.71).aspx

MSDN 上 web.config 的全球化元素:https: //msdn.microsoft.com/en-us/library/ydkak5b9(v=vs.71).aspx