开发者

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. If my server is set to EST, the current server time is 4:00 pm.

I need a way to get the timezone difference between the client and the server, either in Javascript or C#. In my example, I would get 3 (or -3, doesn't matter).

Does anyone know how to do this?

EDIT: Possible so开发者_如何学Golution for RedFilter

Doing it all in 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.
2 - Create a new javascript date client side (var currentTime = new Date();) and subtract the above date
3 - Post the result back to the server (if necessary; you may only need to know the difference client-side).

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. 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.

//This was useful for me - 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). 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).

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. E.g. currently I'm in Irish Summer Time (GMT + 1hour daylight saving time), and it returns -60.

You can easily put that in a URI used by an image or XHR request, or put it in a cookie value.


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. 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? 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).

Note that this is running server-side (but the reverse will obviously work) and that my server runs 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.

Convert the client and server time with the associated zone to UTC (ConvertTimeZoneToUtc) and compare the two.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜