获取时区名称 [英] Get name of time zone

查看:109
本文介绍了获取时区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用jQuery获得客户端所在时区的名称吗?

Can I get the name of the time zone of the client by using jQuery?

示例:太平洋地区标准时间,格林威治标准时间,例如: https://en.wikipedia. org/wiki/List_of_time_zones_by_country

Example: Pacific S.A. Standard Time, Greenwich Standard Time, like this: https://en.wikipedia.org/wiki/List_of_time_zones_by_country

时区列表

推荐答案

我可以使用jQuery获得客户端所在时区的名称吗?

Can I get the name of the time zone of the client by using jQuery?

不. jQuery只有一个日期/时间函数,即$.now().

No. jQuery has only one date/time function, which is $.now().

如果您的意思是 JavaScript ,答案仍然是.您只能从date.getTimezoneOffset()获取时区偏移.您无法获得时区-至少不是在所有浏览器中都可以.请参阅时区标签Wiki 的标题为"Time Zone != Offset"

If you meant JavaScript, the answer is still no. You can only obtain a time zone offset from date.getTimezoneOffset(). You can't get a time zone - at least not in all browsers. Refer to the timezone tag wiki's section titled: "Time Zone != Offset"

通过使用 jsTimeZoneDetect 库,您可以在时区猜测 ,但这只是一个猜测.它可能正确也可能不正确.

You can guess at the time zone, by using the jsTimeZoneDetect library, but it is just a guess. It may or may not be accurate.

您现在还可以将 moment.js

You can also now use moment.js with the moment-timezone add on. It now supports time zone guessing with moment.tz.guess().

如果您可以保证您的用户在完全支持 ECMAScript国际化的环境中运行API ,您可以像这样获取用户的时区:

If you can guarantee your users are running in an environment that fully supports the ECMAScript Internationalization API, you can get the user's time zone like this:

Intl.DateTimeFormat().resolvedOptions().timeZone

您可以查看兼容性表,在DateTimeFormat-resolvedOptions().timezone defaults to the host environment下,确定将在哪种环境下工作.

You can review the compatibility table, under DateTimeFormat - resolvedOptions().timezone defaults to the host environment to determine which environments this will work in.

老实说,最好的办法就是在用户可以选择时区的地方显示一个屏幕.您可以使用下拉列表,也可以使用基于地图的时区选择器-像这样.您可以使用jsTimeZoneDetect作为默认值,但是您的用户应该可以更改它.

Honestly, the best thing to do is to just give your user a screen somewhere that they can select their timezone. You might use a drop-down list, or you might use a map-based timezone picker - like this one. You can use jsTimeZoneDetect as a default value, but your user should be able to change it.

此外,所有这些都将为您提供IANA时区标识符,例如America/Los_Angeles.但是您提供的示例似乎是Windows时区ID(与.net中的TimeZoneInfo一起使用).您应该阅读时区标签wiki ,然后阅读以下问题:

Also, all of these are going to give you an IANA time zone identifier, such as America/Los_Angeles. But the examples you gave appear to be Windows time zone ids (for use with TimeZoneInfo in .net). You should read the timezone tag wiki, and then also this question: How to translate between Windows and IANA time zones?

这篇关于获取时区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆