如何从GMT转换到EST时区

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

How to convert from GMT to EST Timezone

问题

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

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

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

英文:
  1. $url = "http://feeds.marketwatch.com/marketwatch/marketpulse"
  2. [xml]$xml = (new-object System.Net.WebClient).DownloadString($url)
  3. $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的示例,查看更多信息,请参阅此链接

  1. $xml.rss.channel | % {$_.item} |
  2. 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

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

答案2

得分: 1

  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

  1. <details>
  2. <summary>英文:</summary>
  3. 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

  1. </details>
  2. # 答案3
  3. **得分**: -1
  4. 不需要转换。只需确保您从XML中将日期作为字符串读取,然后将其转换为DateTime。请尝试以下代码:
  5. ```powershell
  6. using assembly System.Xml.Linq
  7. $uri = 'http://feeds.marketwatch.com/marketwatch/marketpulse'
  8. $doc = [System.Xml.Linq.XDocument]::Load($uri)
  9. $items = $doc.Descendants('item')
  10. $table = [System.Collections.ArrayList]@()
  11. foreach ($item in $items) {
  12. $title = $item.Element('title').Value
  13. $link = $item.Element('link').Value
  14. $description = $item.Element('description').Value
  15. $pubDate = [DateTime]$item.Element('pubDate').Value
  16. $guid = $item.Element('guid').Value
  17. $newRow = [psobject]@{
  18. 'Title' = $title
  19. 'Link' = $link
  20. 'Description' = $description
  21. 'Pub Date' = $pubDate
  22. 'guid' = $guid
  23. }
  24. $table.Add($newRow) | Out-Null
  25. }
  26. $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 -->

  1. using assembly System.Xml.Linq
  2. $uri = &#39;http://feeds.marketwatch.com/marketwatch/marketpulse&#39;
  3. $doc = [System.Xml.Linq.XDocument]::Load($uri)
  4. $items = $doc.Descendants(&quot;item&quot;)
  5. $table = [System.Collections.ArrayList]@()
  6. foreach($item in $items)
  7. {
  8. $title = $item.Element(&#39;title&#39;).Value
  9. $link = $item.Element(&#39;link&#39;).Value
  10. $description = $item.Element(&#39;description&#39;).Value
  11. $pubDate = [DateTime]$item.Element(&#39;pubDate&#39;).Value
  12. $guid = $item.Element(&#39;guid&#39;).Value
  13. $newRow = [psobject]@{
  14. &#39;Title&#39;=$Title
  15. &#39;Link&#39;=$link
  16. &#39;Description&#39;=$description
  17. &#39;Pub Date&#39;=$pubDate
  18. &#39;guid&#39;=$guid
  19. }
  20. $table.Add($newRow) | out-null
  21. }
  22. $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:

确定