如何在JavaScript中仅获取时区名称?

3
我已经尝试了下面的代码,但它们不起作用,因为它们包括日期和/或时间。我只需要获取时区名称。(注意:时区名称,而不是IANA时区)
是否可以强制toLocaleString()同时显示"timeZoneName"的"short"和"long"?
例如:2021年5月2日,02:34:28 GMT+12:45查塔姆标准时间 Get_TimeZone_Name(Functions)适用于现代浏览器(2017-2018+)和IE11!

<!DOCTYPE html>

<div id="Output"></div>

<script>

Date_Object = new Date();

document.getElementById("Output").innerText =
Date_Object.toLocaleString([], {timeZoneName:"short"}) + "\n" +
Date_Object.toLocaleDateString([], {timeZoneName:"short"}) + "\n" +
Date_Object.toLocaleTimeString([], {timeZoneName:"short"}) + "\n" +
"\n" +
Date_Object.toLocaleString([], {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString([], {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString([], {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("en-US", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("en-US", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("en-US", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("pt-BR", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("pt-BR", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("pt-BR", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("ja-JP", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("ja-JP", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("ja-JP", {timeZoneName:"long"}) + "\n" +
"\n" +
Date_Object.toLocaleString("ar-SA", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleDateString("ar-SA", {timeZoneName:"long"}) + "\n" +
Date_Object.toLocaleTimeString("ar-SA", {timeZoneName:"long"}) + "\n" +
"";

</script>

2个回答

5

在这里,你只需要4个步骤:

  1. 获取简单日期字符串
  2. 获取长时区名称
  3. 获取短时区名称
  4. 将它们拼接在一起

see the code below

let date = new Date();

let dateString = date.toLocaleString();

let shortTimeZone = getTimeZoneName(date,[],'short');

let longTimeZone = getTimeZoneName(date,[],'long');

console.log(`${dateString} ${shortTimeZone} ${longTimeZone}`)
  
/**
* date: Date = date object
* locales: string | [] = 'en-us' | []
* type: string = 'short' | 'long'
**/
function getTimeZoneName(date,locales,type) {
 return new Intl.DateTimeFormat(locales, { timeZoneName: type })
  .formatToParts(date)
  .find(part => part.type == "timeZoneName")
  .value
}

我从ThirstyMonkey那里得到了这个想法。

您可以在MDN上找到更多Intl.DateTimeFormat的用法。


3
你混淆了时区和区域设置。 en-us 是一种区域设置,而不是一个时区。 - Matt Johnson-Pint
1
是的,它有效,谢谢!(不过,“formatToParts()”只在现代浏览器2017-2018+中有效,并且如@MattJohnson-Pint所说,应该用“Locale”替换“timeZone”参数,还应添加一个新参数“Zone”,并与{timeZone:Zone, timeZoneName:type}一起使用) - user
1
谢谢你指出这个问题,我会修复它。 - YEN YEE

1

无法强制 .toLocaleString 同时显示短时区和长时区,不过可以尝试使用以下解决方案。

Date_Object = new Date();
myTimezoneFormat(Date_Object)

这里是myTimezoneFormat函数

function myTimezoneFormat(date, locale="us") {
  return date.toLocaleString(locale, {timeZoneName:"short"}).split(" ").slice(2).join(" ")+" "+ date.toLocaleString(locale, {timeZoneName:"long"}).split(" ").slice(2).join(" ");
}

2
谢谢回答,但这只适用于“我们”的地区设置。 - user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接