Reputation: 313
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 ?
Upvotes: 1
Views: 4816
Reputation: 619
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);
Upvotes: 1
Reputation: 115328
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 compare 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.
Upvotes: 4