我知道如何获取时区偏移量,但我需要的是能够检测到类似于“美国/纽约”的内容。这是否可能通过JavaScript实现,还是我需要根据偏移量进行猜测?
我知道如何获取时区偏移量,但我需要的是能够检测到类似于“美国/纽约”的内容。这是否可能通过JavaScript实现,还是我需要根据偏移量进行猜测?
国际化 API 支持获取用户的时区信息,并且在所有当前浏览器中都得到支持。
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
timeZone
属性设置为 undefined
而不是用户的时区字符串。据我所知,截至撰写本文时(2017 年 7 月),除了 IE11 外的所有当前浏览器 都会返回用户时区作为字符串。undefined
:let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
- ViaTechlet
anything will return undefined
, you need to evaluate timezone
, or only run Intl.DateTimeFormat().resolvedOptions().timeZone
- Daniel ComptonIntl.DateTimeFormat().resolvedOptions().timeZone
根据定义返回的是IANA时区名称,该名称是用英语表示的。Date
的字符串表示中解析出来。
function getTimezoneName() {
const today = new Date();
const short = today.toLocaleDateString(undefined);
const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });
// Trying to remove date from the string in a locale-agnostic way
const shortIndex = full.indexOf(short);
if (shortIndex >= 0) {
const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
// by this time `trimmed` should be the timezone's name with some punctuation -
// trim it from both sides
return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');
} else {
// in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
return full;
}
}
console.log(getTimezoneName());
GMT+07:00
)而不是名称。但我认为作为备选方案,它仍然可读。Intl...
解决方案一样。timeZoneName: 'short'
来获取简短形式,如 EST 或 GMT+0230,我可以在 Java 后端中使用。 - John Yeary当前用户语言的简短可能结果:
console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4));
PDT
或EST
)的人,您可以使用此变体:
new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'short' }).substring(4) - ninapavlichIntl.DateTimeFormat().resolvedOptions().timeZone
- user2923322Intl.DateTimeFormat().resolvedOptions().timeZone
返回undefined
,但moment.tz.guess()
返回正确的值。 - Antoni4jstz.olson.timezones
变量中获取时区列表。var tz = jstz.determine();
tz.name();
欢迎使用jstz!
console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4).match(/\b(\w)/g).join(''))
你可以通过使用这里的映射表来编写自己的代码: http://www.timeanddate.com/time/zones/
或者,使用moment-timezone库: http://momentjs.com/timezone/docs/
查看 zone.name; // America/Los_Angeles
或者,使用这个库: https://github.com/Canop/tzdetect.js
要检测类似于 "America/New York." 这样的内容,你可以使用 Luxon 库 中的 new LocalZone()
方法。
import { LocalZone } from 'luxon';
const zoneName = new LocalZone().name;
在旧版 JavaScript 中获取时区代码(例如 GMT
)(我正在使用带有旧引擎的 Google 应用脚本):
function getTimezoneName() {
return new Date().toString().get(/\((.+)\)/);
}
我只是把这个放在这里,以防有人需要它。
UTC
/GMT
。其他时区似乎是全拼的,例如太平洋夏令时
。 - Ian Dunnvar x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
Moment时区将是一个有用的工具。 http://momentjs.com/timezone/
在不同时区之间转换日期
var newYork = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london = newYork.clone().tz("Europe/London");
newYork.format(); // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format(); // 2014-06-01T17:00:00+01:00