Can I get the browser time zone in ASP.NET or do I have to rely on JS operations to retrieve the information?

前端 未结 4 1387
[愿得一人]
[愿得一人] 2020-12-02 14:49

Can I get the browser time zone in ASP.NET or do I have to rely on JS operations to retrieve the information

4条回答
  •  孤城傲影
    2020-12-02 15:31

    You can't get reliably get the client's timezone settings from the browser at the client or server side.

    You can, from JavaScript, get the offset of the current timezone from UTC, using Date#getTimezoneOffset. This doesn't uniquely identify a timezone, though, as there are many zones that may use the same UTC-offset, plus many regions that switch timezones on DST.

    You can sometimes get a string representing the timezone's short name by calling Date#toLocaleString. There's no guarantee there will be a usable timezone name, and timezone names aren't globally-unique, and sometimes the browser lies (especially about DST). But often there's something, and if you can tie that abbreviation up with the offset you've already read, and compare that against a shortlist of known timezone rules, it gives you a good first guess. You can also throw geographical IP-targeting and server-side use of the Accept-Language header in to help guess better.

    If it's important for users to see appropriate local times, you'll need to give them a manual mechanism to choose a timezone-rules locale (eg. as a per-account setting), because none of this is reliable.

提交回复
热议问题