代码之家  ›  专栏  ›  技术社区  ›  Chris Barr

如何从Intl.DateTimeFormat中只获取时区名称?

  •  1
  • Chris Barr  · 技术社区  · 8 月前

    我正在尝试生成一个日期对象,然后将各种格式化的部分保存到变量中。所有这些都起作用,除了我似乎不能只生成/格式化时区名称本身。

    以下代码将产生 3/20/2024, EDT 但我只想 EDT 待生产。

    const timeZoneFormatter1 = new Intl.DateTimeFormat("en-US", {
      timeZoneName: "short",
      timeZone: "America/New_York",
    });
    console.log(timeZoneFormatter1.format(now));
    

    我缺少什么选择?如何省略日期信息,只生成时区名称?

    我想我可以手动获取 '3/20/2024, EDT'.split(',')[1].trim() 但这让我觉得很生气。

    这是我尝试的一个工作演示

    const now = new Date();
    const locale = "en-US";
    const timeZone = "America/New_York"; //always use HQ local time zone
    
    const dateFormatter = new Intl.DateTimeFormat(locale, {
      weekday: "short",
      month: "short",
      day: "numeric",
      timeZone,
    });
    console.log('Date:', dateFormatter.format(now));
    
    const timeFormatter = new Intl.DateTimeFormat(locale, {
      hour: "numeric",
      minute: "numeric",
      second: "numeric",
      timeZone,
    });
    console.log('Time:', timeFormatter.format(now));
    
    const timeZoneFormatter1 = new Intl.DateTimeFormat(locale, {
      timeZoneName: "short",
      timeZone,
    });
    console.log('TimeZone attempt #1:', timeZoneFormatter1.format(now));
    
    const timeZoneFormatter2 = new Intl.DateTimeFormat(locale, {
      month: undefined,
      day: undefined,
      year: undefined,
      timeZoneName: "short",
      timeZone,
    });
    console.log('TimeZone attempt #2:', timeZoneFormatter2.format(now));
    
    const timeZoneFormatter3 = new Intl.DateTimeFormat(locale, {
      timeZoneName: "short",
      timeZone,
    });
    console.log('TimeZone attempt #3 (hacky):', timeZoneFormatter2.format(now).split(',')[1].trim());
    2 回复  |  直到 8 月前
        1
  •  1
  •   phuzi Frank Heikens    8 月前

    您可以使用 DateTimeFormat.formatToParts() 并使用的值 timeZoneName 部分

    这比任何字符串操作都要简单得多。

    const now = new Date();
    const locale = "en-US";
    const timeZone = "America/New_York"; //always use HQ local time zone
    
    const dateFormatter = new Intl.DateTimeFormat(locale, {
      weekday: "short",
      month: "short",
      day: "numeric",
      timeZone,
    });
    console.log('Date:', dateFormatter.format(now));
    
    const timeFormatter = new Intl.DateTimeFormat(locale, {
      hour: "numeric",
      minute: "numeric",
      second: "numeric",
      timeZone,
    });
    console.log('Time:', timeFormatter.format(now));
    
    const timeZoneFormatter1 = new Intl.DateTimeFormat(locale, {
      timeZoneName: "short",
      timeZone,
    });
    
    const parts = timeZoneFormatter1.formatToParts(now);
    console.log('parts:', parts);
    
    const tzName = parts.find(x => x.type === 'timeZoneName').value;
    console.log('timeZoneName:', tzName);
        2
  •  -2
  •   Shailendra Sharma    8 月前

    Intl.DateTimeFormat构造函数不直接支持仅格式化时区名称而不包括其他日期或时间组件。

    但是,您可以通过格式化仅包括时区信息的固定日期,然后从中提取时区名称来实现这一点。

    Working Link

    const now = new Date();
    const locale = "en-US";
    const timeZone = "America/New_York"; // Always use HQ local time zone
    
    // Format a fixed date (Unix epoch) to get only the time zone name
    const timeZoneFormatter = new Intl.DateTimeFormat(locale, {
      timeZoneName: "short",
      hour: "numeric",
      minute: "numeric",
      second: "numeric",
      hour12: false, // Use 24-hour format to avoid ambiguity
      timeZone,
    });
    
    // Format a fixed date (Unix epoch) to get only the time zone name
    const timeZoneName = timeZoneFormatter.format(0).split(' ')[1]; // Extracting the time zone name
    
    console.log('Time Zone:', timeZoneName);