英文:
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.
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 = '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
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论