Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 solution 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?

like image 248
Steven Avatar asked Sep 01 '10 19:09

Steven


People also ask

How do you determine client time zone?

The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes. This offset is changed by dividing by 60 and negating the result.

How do I get different time zones in C#?

To display all time zones in our current system, we use TimeZoneInfo. GetSystemTimeZone() static method that returns all available time zones on a machine. The DisplayName property is the name of the time zone.

How do I find my timezone in JavaScript?

JavaScript Date getTimezoneOffset() getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

What is server and UTC time?

If you've ever had to work with servers, you're probably familiar with UTC time, also known as Coordinated Universal Time. UTC time is the standardized time used by most computers and servers around the world. On some level, any time your server does anything at a specific time or on a schedule, you're using UTC time.


2 Answers

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

like image 119
Travis Avatar answered Sep 21 '22 15:09

Travis


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")

like image 42
D'Arcy Rittich Avatar answered Sep 22 '22 15:09

D'Arcy Rittich