views:

176

answers:

3

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?

+7  A: 

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:

<script>
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);
</script>

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.

RedFilter
I edited my original, let me know if you think that would work.
Steven
@Steven: see my update
RedFilter
Thank you, this did end up working.
Steven
A: 

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.

Khan
That's the problem, I don't know the timezone that the client will be navigating from, it could be from anywhere. If I could get that info somehow, that would be helpful, but I don't know how.
Steven
Then I would like to know how you obtain the time from the client. Cause I have a similar problem and I resolved it by using the location of all the contacts. I use cities location associated with a timezone.
Khan
A: 

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.

Jon Hanna