问题
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:
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.
回答2:
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;
回答3:
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).
来源:https://stackoverflow.com/questions/1017053/finding-the-clients-timezone-and-culture-via-web-service