如何从GMT转换到EST时区

huangapple go评论66阅读模式
英文:

How to convert from GMT to EST Timezone

问题

以下是翻译好的代码部分:

$url = "http://feeds.marketwatch.com/marketwatch/marketpulse"
[xml]$xml = (new-object System.Net.WebClient).DownloadString($url)
$xml.rss.channel | Foreach {$_.item} | Format-Table pubdate, title

如果您有任何其他问题,请随时提出。

英文:
$url = "http://feeds.marketwatch.com/marketwatch/marketpulse"
[xml]$xml = (new-object System.Net.WebClient).DownloadString($url)
$xml.rss.channel | Foreach {$_.item}  | Format-Table pubdate, title

I have successfully parsed the XML into a clean table.
如何从GMT转换到EST时区
How do I convert the datetime to EST from GMT?

Any help would be appreciated. Thank you for your time.

答案1

得分: 2

将其转换为Datetime,使用计算属性并按需格式化,参见MM/dd/yyyy的示例,查看更多信息,请参阅此链接

$xml.rss.channel | % {$_.item} | 
select @{N="pubdate";E={([datetime]$_.pubdate).ToString("MM/dd/yyyy")}},title
英文:

Cast to Datetime, use a Calculated property and format it as needed, see example for MM/dd/yyyy, see this link for more information

$xml.rss.channel | % {$_.item} | 
select @{N="pubdate";E={([datetime]$_.pubdate).ToString("MM/dd/yyyy")}},title

答案2

得分: 1

解决方案

$url = "http://feeds.marketwatch.com/marketwatch/marketpulse"
[xml]$xml = (new-object System.Net.WebClient).DownloadString($url)
$xml.rss.channel | Foreach {$.item} | select @{N="pubdate";E={[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([datetime]$.pubdate, 'Eastern Standard Time').ToString("ddd MMM d HH:mm:ss")}},title


<details>
<summary>英文:</summary>

Solution 

$url = "http://feeds.marketwatch.com/marketwatch/marketpulse"
[xml]$xml = (new-object System.Net.WebClient).DownloadString($url)
$xml.rss.channel | Foreach {$.item} | select @{N="pubdate";E={[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([datetime]$.pubdate, 'Eastern Standard Time').ToString("ddd MMM d HH:mm:ss")}},title


</details>



# 答案3
**得分**: -1

不需要转换。只需确保您从XML中将日期作为字符串读取,然后将其转换为DateTime。请尝试以下代码:

```powershell
using assembly System.Xml.Linq

$uri = 'http://feeds.marketwatch.com/marketwatch/marketpulse'
$doc = [System.Xml.Linq.XDocument]::Load($uri)
$items = $doc.Descendants('item')

$table = [System.Collections.ArrayList]@()

foreach ($item in $items) {
    $title = $item.Element('title').Value
    $link = $item.Element('link').Value
    $description = $item.Element('description').Value
    $pubDate = [DateTime]$item.Element('pubDate').Value
    $guid = $item.Element('guid').Value

    $newRow = [psobject]@{
        'Title' = $title
        'Link' = $link
        'Description' = $description
        'Pub Date' = $pubDate
        'guid' = $guid
    }
    $table.Add($newRow) | Out-Null
}
$table | Format-Table

这段代码的作用是从指定的XML源加载数据,并将日期字段转换为DateTime类型,然后将结果以表格格式输出。

英文:

No need to convert. Just make sure you read the date as a string from xml and convert to DateTime. Try following :

<!-- begin snippet: js hide: false console: true babel: false -->

using assembly System.Xml.Linq

$uri = &#39;http://feeds.marketwatch.com/marketwatch/marketpulse&#39;
$doc = [System.Xml.Linq.XDocument]::Load($uri)
$items = $doc.Descendants(&quot;item&quot;)

$table =  [System.Collections.ArrayList]@()

foreach($item in $items)
{
   $title = $item.Element(&#39;title&#39;).Value
   $link = $item.Element(&#39;link&#39;).Value
   $description = $item.Element(&#39;description&#39;).Value
   $pubDate = [DateTime]$item.Element(&#39;pubDate&#39;).Value
   $guid = $item.Element(&#39;guid&#39;).Value

   $newRow = [psobject]@{
      &#39;Title&#39;=$Title
      &#39;Link&#39;=$link
      &#39;Description&#39;=$description
      &#39;Pub Date&#39;=$pubDate
      &#39;guid&#39;=$guid
   }
   $table.Add($newRow) | out-null
}
$table | Format-Table

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月31日 20:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定