简体   繁体   English

如何在用户时区显示时间?

[英]How do i display the time in the user time zone?

On my site the only place i use time is when i insert into the DB.在我的网站上,我唯一使用时间的地方是我插入数据库时​​。 When i pull it i would like to display the time according to the user time zone.当我拉它时,我想根据用户时区显示时间。 I am generating this part of the page in C# (served in ASP.NET but not using .aspx code files).我正在用 C# 生成页面的这一部分(在 ASP.NET 中提供,但不使用 .aspx 代码文件)。

How do i display the time according to the user time zone?如何根据用户时区显示时间? I have a feeling i need to have the user input his location before i can do this?我有一种感觉,我需要让用户输入他的位置才能执行此操作? Whats the best way to get time from location and take daylights saving into consideration?从位置获取时间并考虑夏令时的最佳方法是什么? or cheat by translating UTC to his via JS and jquery?或者通过 JS 和 jquery 将 UTC 翻译成他的作弊?

You can use the Javascript Date object to get this.您可以使用 Javascript Date 对象来获取它。 Date.getTimezoneOffset() will give you the difference in minutes between local and UTC representations of the date. Date.getTimezoneOffset() 将为您提供本地和 UTC 日期表示之间的分钟差。 The actual value is derived ultimately from the system clock, which, if set correctly, will know what time zone it is in.实际值最终来自系统时钟,如果设置正确,将知道它所在的时区。

  1. You can either ask the user to choose the timezone as you suggested.您可以要求用户按照您的建议选择时区。

  2. You can use (new Date).getTimezoneOffset();您可以使用(new Date).getTimezoneOffset(); as suggested in Robusto's answer .正如Robusto 的回答中所建议的那样。 This will return the number of minutes to subtract from the user's local time to get UTC.这将返回从用户的本地时间中减去以获得 UTC 的分钟数。 The user must have configured the correct timezone in the operating system for this give good results.用户必须在操作系统中配置正确的时区才能获得良好的结果。

  3. Another trick to "guess" the user timezone by doing a client-side date comparison in JavaScript between new Date();另一个通过在 JavaScript 中在new Date();之间进行客户端日期比较来“猜测”用户时区的技巧new Date(); and a timestamp generated by the server.和服务器生成的时间戳。

    The advantage of this is that it would work even if the user would not have configured the correct regional settings in the operating system.这样做的好处是,即使用户没有在操作系统中配置正确的区域设置,它也能工作。 As long as the computer clock of the user is accurate to +/- 30 minutes, it should be reliable.只要用户的电脑时钟精确到+/- 30分钟,就应该是可靠的。

Simply pass the UTC time to the JavaScript Date() constructor.只需将 UTC 时间传递给 JavaScript Date()构造函数。 The resulting Date object will be the time in the user's local timezone, based on their computer's settings.根据用户计算机的设置,生成的Date对象将是用户本地时区的时间。

var gmt = new Date('Feb, 19 2010 13:00:00 GMT');
alert(gmt);

In Chrome, in the Pacific timezone, this displays在 Chrome 中,在太平洋时区,这会显示

Fri Feb 19 2010 05:00:00 GMT-0800 (Pacific Standard Time)

If you need to have this information server-side, don't bother asking the user for their location.如果您需要在服务器端获得此信息,请不要向用户询问他们的位置。 Just ask them for their timezone directly.只需直接询问他们的时区即可。 Lots of sites (particularly forums) already do this.许多网站(特别是论坛)已经这样做了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM