使用 javascript 获得 UTC + 1 次

Get UTC + 1 time with javascript

var options = { timeZone: 'UTC', timeZoneName: 'short'};
const today = new Date();
$w("#pikoTimer").text = today.toLocaleTimeString("en-GB", options);

这是我想使用的代码,所以当我单击一个按钮时,它会将文本设置为 UTC 时间 - 使用 UTC 时它工作正常,但我想将它设置为 UTC + 1,但我找不到解决方案。

当我尝试 timeZone: 'UTC+1' 时,它给了我一个错误提示:RangeError: Invalid time zone specified: UTC+1

您应该在选项的 timeZone 字段中指定时区名称,例如 timeZone: 'Australia/Sydney'timeZone: 'EST'utc+1 是无效的时区名称。在 mdn Intl/DateTimeFormat 中阅读更多内容;您可以像这样使用带有您想要的时区的城市名称:

const options = { timeZone: 'Europe/London', timeZoneName: 'short'};
const today = new Date();
console.log(today.toLocaleTimeString("en-GB", options))

ECMAScript 语言规范规定(ECMA-262 section 20.4.4.40) that the options parameter of the Date.prototype.toLocaleTimeString() function is to be implemented as specified in ECMA-402(ECMAScript 国际化API规范)。

根据 ECMA-402,timeZone 选项如果符合 section 6.4(“时区名称”)则有效,其中规定:

The ECMAScript […] Internationalization API Specification identifies time zones using the Zone and Link names of the IANA Time Zone Database. Their canonical form is the corresponding Zone name in the casing used in the IANA Time Zone Database.

这就是为什么您的代码在使用 UTC(在 IANA Time Zone Database 中是 link 到 Etc/UTC)时有效,但在使用 UTC+1(这不是有效的 IANA 时区)。

相反,您应该使用代表相同 UTC+01:00 offset 的值,并观察所需的夏令时调整。例如,您可以使用 Europe/ParisEurope/Belgrade.

const options = { timeZone: 'Europe/Paris', timeZoneName: 'short'};
const today = new Date();
$w("#pikoTimer").text = today.toLocaleTimeString("en-GB", options);

请参阅 List of tz database time zones 以获取方便的在线参考。