You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中如何将XML中的GMT时间转换为EST时区

解决GMT转EST时区的PowerShell实现

要把XML返回的GMT格式pubdate转换成EST(美国东部标准时间),需要先将字符串解析为带时区信息的DateTime对象,再进行时区转换,具体修改代码如下:

$url = "http://feeds.marketwatch.com/marketwatch/marketpulse"
[xml]$xml = (New-Object System.Net.WebClient).DownloadString($url)

# 定义目标时区:美国东部时区(自动处理EST/EDT夏令时切换)
$estTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

$xml.rss.channel.item | ForEach-Object {
    # 将GMT时间字符串解析为UTC DateTime对象
    $utcDate = [DateTime]::SpecifyKind([DateTime]::Parse($_.pubdate), [DateTimeKind]::Utc)
    # 转换为EST时区时间
    $estDate = [System.TimeZoneInfo]::ConvertTimeFromUtc($utcDate, $estTimeZone)
    # 输出处理后的对象
    [PSCustomObject]@{
        PubDateEST = $estDate.ToString("yyyy-MM-dd HH:mm:ss") # 可自定义时间格式
        Title = $_.title
    }
} | Format-Table PubDateEST, Title -AutoSize

关键说明:

  • 时区ID选择:使用Eastern Standard Time作为时区ID,PowerShell会自动识别夏令时(切换为EDT),无需手动区分EST/EDT。
  • DateTime解析:通过SpecifyKind明确标记解析后的时间为UTC,避免本地时区干扰。
  • 时间格式自定义:ToString("yyyy-MM-dd HH:mm:ss")可以根据需求调整格式,比如保留原RSS的时间格式可改为"r"(RFC1123格式)。

如果不需要自定义格式,直接输出$estDate即可,Format-Table会默认显示合适的时间样式。

内容的提问来源于stack exchange,提问作者iceman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 10:32:38