开发者

How to find the client machine time zone using ASP.NET

开发者 https://www.devze.com 2023-02-26 19:33 出处:网络
I need to find the time zone of the client machine using ASP.NET (C#) or JavaScript. What are the different time zones that are available all over the world and how to convert the date and time based

I need to find the time zone of the client machine using ASP.NET (C#) or JavaScript. What are the different time zones that are available all over the world and how to convert the date and time based on the users time z开发者_如何学JAVAone.

Please provide some suggestions or sample coding to change the time based on the time zone.


You cannot find client time zone settings from ASP.NET.

You can use JavaScript to tell the current time, but there are several time zones that can be synchronized at any given time.

In Chrome, you can get the time zone from the JavaScript date object. There's no specific function for it, from what I've found, but the code

(new Date()).toString()

will yield something like

Mon Apr 18 2011 08:58:59 GMT+0200 (W. Europe Daylight Time)

In websites, the best approach I've found has been to have a setting for each user to specify the time zone to display all times in. If the JavaScript getUtcOffset gives a different offset than what is expected for the user's time zone, I'll show a notice for the user to review their settings. If time zone can be guessed from the date (which I've only found to be the case in chrome), I'll suggest that time zone, but I still resort to a select box for the user to manually pick the time zone.

Even so, it is possible to have the wrong time zone setting, without the script noticing it, because for a great part of the year, the two time zones may be perfectly synchronized.

Once you have a time zone (you can enumerate them all with System.TimeZoneInfo.GetSystemTimeZones()), you can convert UTC dates with System.TimeZoneInfo.ConvertTimeFromUtc and System.TimeZoneInfo.ConverTimeToUtc, respectively.

0

精彩评论

暂无评论...
验证码 换一张
取 消