【发布时间】:2021-07-24 06:07:59
【问题描述】:
我已经尝试过下面的代码,但它们不起作用,因为它们包含日期和/或时间。我只需要获取时区名称。 (OBS:时区名称,而不是 IANA 时区)
是否可以强制 toLocaleString() 同时显示 timeZoneName "short" 和 "long"?
示例:2021 年 5 月 2 日,格林威治标准时间 02:34:28 + 查塔姆标准时间 12:45
Get_TimeZone_Name(Functions) for Modern Browsers (2017-2018+) and for 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>
【问题讨论】:
标签: javascript date time timezone locale