9

我正在尝试更改我的 asp.net 网站的默认时区,我尝试了以下代码,但没有成功

<system.web>
<globalization culture="ar-JO" uiCulture="ar-JO" />
<httpRuntime maxUrlLength="10999" maxQueryStringLength="2097151" />
<compilation debug="true" targetFramework="4.0"/>
<customErrors mode="Off"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

4

2 回答 2

24

改变文化不会改变时区。.NET 中无法根据每个应用程序更改时区。它只能在系统范围内更改。

许多服务器将其设置为 UTC,但最佳做法是完全不依赖系统时区。切勿在 Web 应用程序中使用DateTime.Now, TimeZoneInfo.Local,DateTimeKind.Local等。

相反,请使用DateTime.UtcNow, DateTimeOffset.UtcNow,或者如果您必须知道服务器的本地时间,请使用DateTimeOffset.Now.

如果您需要特定用户的本地时间,那么您需要知道他们的时区,并使用 on 函数在TimeZoneInfo该特定时区和 UTC 之间进行转换。

在这里阅读更多。

于 2013-10-08T17:27:16.273 回答
1

您将需要更改托管数据库的服务器的时区。您无法通过 web.config 进行操作。

以 UTC 格式存储日期/时间是最佳做法。请参阅下面的这篇文章。

http://www.4guysfromrolla.com/articles/081507-1.aspx

希望这可以帮助。

(来源:http ://forums.asp.net/post/5060690.aspx )

于 2013-10-08T12:14:16.993 回答