admin管理员组

文章数量:1432160

How can we measure the time difference between server time and Browser time ? If we are taking the time in millisecond in both browser and server will it give me the accurate result in case server and browser are in two different timezones ?

How can we measure the time difference between server time and Browser time ? If we are taking the time in millisecond in both browser and server will it give me the accurate result in case server and browser are in two different timezones ?

Share Improve this question asked May 8, 2012 at 14:50 neoneo 3131 gold badge6 silver badges19 bronze badges 4
  • Seems like this would depend entirely on what time the user has their clock set to. I can set my clock to any time that I like technically. – BeRecursive Commented May 8, 2012 at 14:51
  • It sounds like you should think about using NTP. – Matt Ball Commented May 8, 2012 at 14:53
  • let's start with how do you figure our the browser time and the server time. would you show us some code please? and also why would you do that. what is it that you're trying to achieve? – Pavel Veller Commented May 8, 2012 at 14:54
  • @BeRecursive : yeah i agree to that point . Still is there a way to get the difference ? – neo Commented May 8, 2012 at 14:55
Add a ment  | 

2 Answers 2

Reset to default 4

There is not built-in way. You should do this on the application level. You can retrieve time using javascript and send this result to server where you can obviously know what time is it. Now pare these results.

The result will be accurate up to the network latency.

If you just want to know the time zone difference it is good enough. If however you want to know the network latency you can estimate it: send client time from client to server, send server time from server to client, take in consideration the time zone offsets. The rest is the client->server->client latency. Divide it by 2 - this is the first estimation of one-way latency. Better estimation requires more statistics.

You can get time-zone GMT offset of client by Javascript Date.getTimezoneOffset(), and save this value to the hidden field. Here is a sample script, can be used to determine time-zone of client:

var now = new Date();
var currentTimeZoneOffset = now.getTimezoneOffset();
var jan = new Date( now.getFullYear(), 0, 1, 2, 0, 0 ), jul = new Date( now.getFullYear(), 6, 1, 2, 0, 0 );
var hemisphere = ( jan.getTime() % 24 * 60 * 60 * 1000 ) > ( jul.getTime() % 24 * 60 * 60 * 1000 );
var dstOffset = hemisphere ? jan.getTimezoneOffset() : jul.getTimezoneOffset();
var standardOffset = hemisphere ? jul.getTimezoneOffset() : jan.getTimezoneOffset();
var gmtHours = -currentTimeZoneOffset/60;
document.write("GMT time-zone offset: " + gmtHours +"<br>");
var isDstActive = (currentTimeZoneOffset-dstOffset) != 0;
document.write("Daylight saving time is active: " + isDstActive + "<br>");
var isTimezoneWithDst = (dstOffset-standardOffset) != 0;
document.write("DST is observed in this timezone: " + isTimezoneWithDst);

本文标签: javaTime difference between server and browserStack Overflow