【问题标题】:How to get only the timeZone name in JavaScript?如何在 JavaScript 中仅获取时区名称?
【发布时间】: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


    【解决方案1】:

    你去,你只需要 4 步:

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

    见下面的代码

    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的更多用法

    【讨论】:

    • 您正在混合时区和语言环境。 en-us 是区域设置,而不是时区。
    • 是的,它有效,谢谢! (虽然,“formatToParts()”仅适用于现代浏览器 2017-2018+,并且正如@MattJohnson-Pint 所说,“timeZone”参数应该替换为“Locale”,并且新参数“Zone”应该是添加和使用 {timeZone:Zone, timeZoneName: type})
    • 感谢您指出这一点,我会解决的。
    【解决方案2】:

    没有办法强制.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(" ");
    }
    

    【讨论】:

    • 感谢您的回答,但这仅适用于“我们”语言环境。
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2013-08-17
    • 2021-11-28
    • 2013-03-03
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多