如何获得服务器的时间转换为本地时间和处理夏令时 [英] How to get convert server time to local time and deal with daylight saving time

查看:2691
本文介绍了如何获得服务器的时间转换为本地时间和处理夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主办Godaddy的我的网站(asp.net web表单,.NET 4.5,SQL Server 2012的),他们(服务器)使用山地标准时间 -7 比较 UTC 时间),这是<强>从来没有改变过和遵守夏令时。

所以,如果我做

 的Response.Write(UTC时间:+ DateTime.UtcNow.ToString()+&LT; BR /&gt;中);
的Response.Write(服务器时间:+ DateTime.Now.ToString()+&LT; BR /&gt;中);
的Response.Write(服务器的DateTimeOffset:+ DateTimeOffset.Now.ToString()+&LT; BR /&gt;中);

它会显示这样的:

  UTC时间:2015年9月18日下午5时14分09秒
服务器时间:2015年9月18日上午10时14分09秒
服务器的DateTimeOffset:2015年9月18日上午10时14分09秒-07:00

但我的用户位于美国佐治亚州亚特兰大其中确实实行夏令时间,根据的 timeanddate.com 他们使用 EDT 在夏 EST 在冬季

我如何获取用户当前时间?让说用户打开我的网络应用程序,并按显示我的时间按钮,它会显示正确的当前用户的时间?


解决方案

  1. 您永远不应该依赖于服务器的时区设置。因此, DateTime.Now 不应该的曾经的使用ASP.NET应用程序。

    有许多其他原因,以避免 DateTime.Now 。阅读:对DateTime.Now的情况下


  2. 服务器的本地时间永远保证是你的网站的用户的本地时间反正。如果是,这只是巧合。


  3. 要获得用户的当前时间在他们的时区,最简单的方法是通过JavaScript:

      VAR =现在新的Date();

    虽然这是基于用户的时钟,其可以或可以不正确地设置。要对时间任何保证,你必须使用服务器的时钟的UTC时间,应用了用户的时区。

    您可以考虑的一种方法是向服务器发送的UTC时间到客户端,然后加载到JavaScript中的客户端的IT项目到他们的时区:

      // C#
    字符串serverUtcTime = DateTime.UtcNow.ToString(O); //2015-09-18T17:53:15.6988370Z// JS
    VAR现在=新的日期(2015-09-18T17:53:15.6988370Z);


  4. 其实检测用户的时区是目前没有解决一个困难的问题。有些人可能会建议新的Date()。使用getTimezoneOffset(),但只给你目前的数字偏移量,而不是时区。偏移可以更改DST,许多时区使用类似的偏移。也有并发症过渡将会对你的工作。

    脚本如 jsTimeZoneDetect 可以的猜测的IANA的时区ID,例如为美国/纽约东部时间,但他们不是100%准确。如果您需要用户的时区的服务器上,那么最终应该的询问的用户某处在您的应用程序的时区。


  5. 在.NET中,可以使用野田佳彦时间与IANA时区工作。野田没有时间,.NET有<$ ​​C $ C>的TimeZoneInfo 类,但它只能在Windows时区工作。


  6. 如果您知道的对某些的是,用户在佐治亚州亚特兰大(这是在美国东部时区),那么你可以这样做:

     的DateTime UTC = DateTime.UtcNow;
    TZ的TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(东部标准时间);
    DateTime的东部= TimeZoneInfo.ConvertTimeFromUtc(UTC,TZ);

    或者,与野田的时间和IANA时区ID:

     即时如今= SystemClock.Instance.Now;
    DateTimeZone TZ = DateTimeZoneProviders.Tzdb [美国/纽约];
    ZonedDateTime东部= now.InZone(TZ);


I host my website (asp.net webforms, .Net 4.5, SQL Server 2012) on Godaddy and they (the server) use Mountain Standard Time (-7 compare to UTC time) which is never changed and does not observe daylight saving time.

So if I do

Response.Write("UTC Time: " + DateTime.UtcNow.ToString() + "<br/>");
Response.Write("Server Time: " + DateTime.Now.ToString() + "<br/>");
Response.Write("Server DateTimeOffset: " + DateTimeOffset.Now.ToString() + "<br/>");

It will display this:

UTC Time: 9/18/2015 5:14:09 PM
Server Time: 9/18/2015 10:14:09 AM
Server DateTimeOffset: 9/18/2015 10:14:09 AM -07:00

But my users are located in Atlanta, GA which does observe daylight saving time and according to timeanddate.com they use EDT in the summer and EST in the winter

How do I get correct current time of user? Let say user open my web-application and hit Show my time button, it will display a correct current user's time?

解决方案

  1. You should never rely on the time zone settings of a server. Therefore DateTime.Now should not be ever used in an ASP.NET application.

    There are many other reasons to avoid DateTime.Now. Read:The case against DateTime.Now.

  2. The local time of the server is never guaranteed to be the local time of the user of your website anyway. If it is, it's just coincidental.

  3. The easiest way to get the user's current time in their time zone is via JavaScript:

    var now = new Date();
    

    Though this is based on the user's clock, which may or may not be correctly set. To have any guarantees about the time, you'd have to use the UTC time of the server's clock, with the user's time zone applied.

    One approach you might consider is to send the UTC time of the server down to the client, then load that into JavaScript in the client to project it to their time zone:

    // C#
    string serverUtcTime = DateTime.UtcNow.ToString("o");  // "2015-09-18T17:53:15.6988370Z"
    
    // JS
    var now = new Date("2015-09-18T17:53:15.6988370Z");
    

  4. Actually detecting the user's time zone is a hard problem that does not currently have a solution. Some may recommend new Date().getTimezoneOffset(), but that only gives you the current numeric offset, not the time zone. Offsets can change for DST, and many time zones use similar offsets. There are also complications for historical dates near DST transitions that will work against you.

    Scripts like jsTimeZoneDetect can guess your IANA time zone ID, such as America/New_York for Eastern time, but they are not 100% accurate. If you need the user's time zone on your server, then ultimately should ask the user for their time zone somewhere in your application.

  5. In .NET, you can use Noda Time to work with IANA time zones. Without Noda Time, .NET has the TimeZoneInfo class, but it can only work with Windows time zones.

  6. If you know for certain that the users are in Atlanta, GA (which is in the US Eastern time zone), then you can do this:

    DateTime utc = DateTime.UtcNow;
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTime eastern = TimeZoneInfo.ConvertTimeFromUtc(utc, tz); 
    

    Or, with Noda Time and IANA time zone IDs:

    Instant now = SystemClock.Instance.Now;
    DateTimeZone tz = DateTimeZoneProviders.Tzdb["America/New_York"];
    ZonedDateTime eastern = now.InZone(tz);
    

这篇关于如何获得服务器的时间转换为本地时间和处理夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆