views:

366

answers:

4

I have a .NET web service.

It serves AJAX requests from web users.

I would simply like to know how to automatically get the user's timezone... Not current time offset, but the actual timezone - like, Central Standard Time is -5:00 right now, but Eastern Standard Time will be -5:00 once daylight savings is over. I want to differentiate these users.

I would also like to know how to get their culture settings ("en-US", etc.) so I can render DateTimes and numbers from my web service to their specific preferences.

Any Javascript or .NET solution will work. Thanks.

Note: Asking the user would be a complete last resort, since it's a web service.

+1  A: 

Including the timezone in the request was the subject of a draft RFC here

Because this is not implemented, the only solution is to get the UTC offset in JavaScript:

// create Date object for current location
d = new Date();
// convert to msec since Jan 1 1970
localTime = d.getTime();
// obtain local UTC offset and convert to msec
localOffset = d.getTimezoneOffset() * 60000;
Chaos
Argh - that's a drag. I wish I lived in the year 3000 A.D. and had all my users' information served like high fructose corn syrup.
Jeff Meatball Yang
+1  A: 

Assuming your clients connect to the webservice from a browser, you can use Request.Headers["Accept-Language"] to detect the browser's culture settings.It is not possible to get the timezone directly from the server, you need some javascript on the client to compute a time difference between their local clock and GMT.

If your webservice is consumed diferently (ie. not from a browser), you will need to add these two pieces of information as parameters to your webservice methods.

Radu094
The Accept-Language header is a good start. Thanks Radu.
Jeff Meatball Yang
Please leave a choice to the user though... Not everyone uses their local language in the browser. Not everyone uses their native language in the country they happen to live in. (for example, I wouldn't like to see Greek-formatted text, just because I happen to login from Greece).
viraptor
+1  A: 

You're best to "ask the user" through parameters to the service. The location of the user doesn't tell you, for instance, that they are only visiting the location and are still operating on their home time; or that they are in a multi-cultural area (e.g, Switzerland) and have chosen the culture you didn't expect (French, when you were expecting German).

John Saunders
@John, thanks, I was hoping there was some new-fangled way to get this data.
Jeff Meatball Yang