【问题标题】:How to change the default timezone of my asp.net website in web.config file如何在 web.config 文件中更改我的 asp.net 网站的默认时区
【发布时间】:2013-10-08 07:38:06
【问题描述】:

我正在尝试更改我的 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>

【问题讨论】:

    标签: asp.net c#-4.0 web-config timezone


    【解决方案1】:

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

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

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

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

    阅读更多here

    【讨论】:

      【解决方案2】:

      您需要更改托管数据库的服务器的时区。你不能通过 web.config 来做。

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

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

      希望这会有所帮助。

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

      【讨论】:

        猜你喜欢
        • 2012-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 2011-09-16
        • 1970-01-01
        • 2023-03-27
        • 2015-09-27
        相关资源
        最近更新 更多