在IIS或web.config中设置不同的时区

fiz*_*mhd 2 c# iis timezone

我在许多地方记录时间

If Request.DynamicSettings.AirlineSettings.AirlineGeneralSettings.TimeLogEnabled Then
                        StartTime = DateTime.Now
                        LogTime(Reflection.MethodBase.GetCurrentMethod.DeclaringType.FullName, Reflection.MethodBase.GetCurrentMethod.Name, StartTime, DateTime.Now, "AB-SCR(I)", 0,)
    End If
Run Code Online (Sandbox Code Playgroud)

我用过的所有地方

DateTime.Now

我现在面临一个问题,我目前正在海湾服务器上托管这个问题,GMT +4:00我需要在Gmt + 3Gmt为另一个国家托管这个相同的项目这个托管我需要时间使用该国家的当地时间记录.

有没有办法做到这一点,而无需修改我的代码的每一行.

我已经看过这篇文章timzone与asp.net,但由于我的服务已经上升,我有很多代码要改变,我正在寻找一个更简单的解决方案.

谢谢.

Mat*_*int 7

一些东西:

  1. 您无法在IIS配置或web.config中更改时区.这不是IIS问题,而是应用程序代码中的问题.

  2. DateTime.Now永远不应该在服务器端应用程序中使用,例如ASP.Net Web应用程序.阅读针对DateTime.Now的案例.

  3. 如果您只计算运行时间,请不要使用DateTime.相反,使用System.Diagnostics.Stopwatch.

    Stopwatch sw = Stopwatch.StartNew();
    // ...do some work ...
    sw.Stop();
    TimeSpan elapsed = sw.Elapsed;  // how long it took will be in the Elapsed property
    
    Run Code Online (Sandbox Code Playgroud)
  4. 如果您确实想要特定时区的当前时间,则需要知道时区标识符.(GMT + 4和GMT + 3不同时区,而是时区偏移 看"时区!=偏移"中的时区标签的wiki).你可以看到通过使用Windows时区的列表TimeZoneInfo.GetSystemTimeZones(),或致电tzutil /l上命令行.

    然后在你的申请中:

    string tz = "Arabian Standard Time";
    DateTime now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
    
    Run Code Online (Sandbox Code Playgroud)

    您应该重构代码,以便在您的LogTime方法中完成.然后,您将只有一个地方为您的应用程序设置时区.