简体   繁体   English

获取客户端和服务器之间的时区差异

[英]Get timezone difference between client and server

If my user is in California and they have their computer set to PST, it's 1:00 pm there. 如果我的用户在加利福尼亚,并且他们的计算机设置为PST,则那是下午1:00。 If my server is set to EST, the current server time is 4:00 pm. 如果我的服务器设置为EST,则当前服务器时间是下午4:00。

I need a way to get the timezone difference between the client and the server, either in Javascript or C#. 我需要一种方法来获得客户端和服务器之间的时区差异,无论是在Javascript还是C#中。 In my example, I would get 3 (or -3, doesn't matter). 在我的例子中,我会得到3(或-3,无所谓)。

Does anyone know how to do this? 有谁知道如何做到这一点?

EDIT: Possible solution for RedFilter 编辑:RedFilter的可能解决方案

Doing it all in javascript: 在javascript中完成所有操作:

serverDate = new Date('<%= DateTime.Now.ToString() %>');
clientDate = new Date();
diffMin = (serverDate.getTime()-clientDate.getTime())*1000*60;  //get difference in minutes

Think that would work? 认为这会奏效吗? Or would both of those return the same time? 或者两者都会同时返回?

You could: 你可以:

1 - Return the server date to the client as a Javascript date variable. 1 - 将服务器日期作为Javascript日期变量返回给客户端。
2 - Create a new javascript date client side ( var currentTime = new Date(); ) and subtract the above date 2 - 创建一个新的javascript日期客户端( var currentTime = new Date(); )并减去上述日期
3 - Post the result back to the server (if necessary; you may only need to know the difference client-side). 3 - 将结果发回服务器(如果需要,您可能只需要了解客户端的区别)。

Update 更新

Here is an example: 这是一个例子:

serverDate = new Date('<%= DateTime.Now.ToString() %>'); 
clientDate = new Date(); 
diffMin = (serverDate.getTime()-clientDate.getTime())/(1000*60);
alert("serverDate: " + serverDate + "\r\n" + "clientDate: " + clientDate + "\r\n" +
  "diffMin: " + diffMin);

If the server and client are on the same machine, you will see a diffMin approaching zero. 如果服务器和客户端在同一台机器上,您将看到diffMin接近零。 There is a slight difference between the dates due to the time between the server-side script generating the date and the browser parsing and executing the javascript. 由于生成日期的服务器端脚本与浏览器解析和执行javascript之间的时间,日期之间存在细微差别。

//This was useful for me - DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") //这对我很有用 - DateTime.Now.ToString(“yyyy-MM-ddTHH:mm:ss”)

Do you need to know the timezone of the location or the machine? 您需要知道位置或机器的时区吗?

If you need to know the timezone of the location then your best bet is to subscribe to a Geo-IP service, and check the IP, then checking on the timezone for that location (there are publicly available databases for that). 如果您需要知道该位置的时区,那么您最好的选择是订阅地理IP服务,并检查IP,然后检查该位置的时区(有公共数据库)。 It's not guaranteed as IP geographic information is not guaranteed (and that's definitely not just a theoretical lack of guarantee, mis-information abounds). 由于IP地理信息不能得到保证,因此无法保证(这绝对不仅仅是理论上缺乏保证,而且信息不足也很多)。

Often though, what you really want is the client machine's timezone setting. 但通常,您真正想要的是客户端计算机的时区设置。 For most services I would find it annoying if I was travelling and had a website think of me to be in a different time to that I was working in (I stick to my home timezone if I'm not out of it for long). 对于大多数服务我觉得如果我在旅​​行并且有一个网站认为我在与我工作的时间不同的时候会感到烦恼(如果我不长时间不使用它,我会坚持我的家乡时区)。

This is easily done client side. 这很容易在客户端完成。 new Date().getTimezoneOffset() returns the number of minutes between UTC and local time. new Date().getTimezoneOffset()返回UTC与本地时间之间的分钟数。 Eg currently I'm in Irish Summer Time (GMT + 1hour daylight saving time), and it returns -60. 例如,目前我在爱尔兰夏令时(GMT + 1小时夏令时),它返回-60。

You can easily put that in a URI used by an image or XHR request, or put it in a cookie value. 您可以轻松地将其放在图像或XHR请求使用的URI中,或者将其放在cookie值中。

This problem has been plaguing me: I'm having a similar issue but I'm doing it server-side and running a node.js AWS Lambda. 这个问题一直困扰着我:我遇到了类似的问题,但我正在服务器端并运行node.js AWS Lambda。 I cannot accept that I need to pass the time from the client to the server (or visa-versa should that be your case). 我无法接受我需要将时间从客户端传递到服务器(如果是这种情况,反之亦然)。 I know the timezone, javascript knows the rules for converting them, why should I need to pass it back and forth? 我知道时区,javascript知道转换它们的规则,为什么我需要来回传递它? What I did just calculates the difference between the two timezones for whatever the date is. 我做的只是计算两个时区之间的差异,无论日期是什么。 Below, it is set up to convert between UTC and timezones in the US, which never have partial hours so I have it set to round to an integer of hours, but you'll need to re-work this if you're ever working with wonky timezones like India, etc. You need to round because there are extra milliseconds leftover (which I suspect has to do with the decay rate of Cesium and not a rounding error--not sure don't care). 下面,它设置为在美国的UTC和时区之间进行转换,从来没有部分时间,所以我将它设置为整数小时,但如果你曾经工作过,你需要重新工作你需要进行回合,因为有剩余的额外毫秒(我怀疑它与铯的衰变率有关,而不是舍入错误 - 不确定不关心)。

Note that this is running server-side (but the reverse will obviously work) and that my server runs UTC. 请注意,这是在服务器端运行(但反过来显然会起作用)并且我的服务器运行UTC。 Also note that when you initialize the first date variable, you need to set the date to the date you want the offset for because they can change day-to-day. 另请注意,初始化第一个日期变量时,需要将日期设置为您希望偏移的日期,因为它们可以每天更改。

var UTCTime = new Date();
var pacificTime = new Date(UTCTime.toLocaleString("en-US",{timeZone: "America/Los_Angeles"}));
var offset = Math.round((UTCTime-pacificTime)/1000/60/60,0);
console.log(offset); // returns 7 (during daylight saving time)

Hope this helps someone... 希望这有助于某人......

One good way to check for timezone difference, is to know the timezone (location) where the time is taken. 检查时区差异的一个好方法是知道时间(时间)所在的时区(位置)。 If you can obtain that on the client side and the server side you can check the TimeZoneInfo class (needs 3.5 I think) TimeZoneInfo from Koders . 如果你可以在客户端和服务器端获得它,你可以检查来自Koders的TimeZoneInfo类(我认为需要3.5) TimeZoneInfo

Convert the client and server time with the associated zone to UTC (ConvertTimeZoneToUtc) and compare the two. 将关联区域的客户端和服务器时间转换为UTC(ConvertTimeZoneToUtc)并比较两者。

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

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