gpt4 book ai didi

powershell - 如何使用 PowerShell 将 UTC 转换为 EST

转载 作者:行者123 更新时间:2023-12-02 18:25:50 28 4
gpt4 key购买 nike

在我的 csv 文件中,我有日期和时间列,但它是 UTC 时间,所以我只是想知道如何使用 PowerShell 将其转换为 EST 时间(12 小时或 24 小时格式)。我一直在 Google 上搜索如何执行此操作,但尚未看到 Excel 列的任何解决方案。


function Convert-JSONColumn {

Write-Host "Start parsing Json Column"

$downloadFile = Import-Csv "C:\AuditLogSearch\Downloaded-Audit-Log-Records.csv"
$modifiedFile = "C:\AuditLogSearch\Modified-Audit-Log-Records.csv"

$downloadFile | ForEach-Object {
$jSON = $_.AuditData | ConvertFrom-Json
$epdata = ""
foreach ($ep in $jSON.extendedProperties) {
$epdata += $ep.Name + " : " + $ep.Value + "`n"
}
$paramdata = ""
foreach ($param in $jSON.Parameters) {
$paramdata += $param.Name + " : " + $param.Value + "`n"
}

New-Object PsObject -Property @{
RecordType = $_.RecordType
UserIds = $_.UserIds
Operations = $_.Operations
CreationTime = $jSON.CreationTime
Operation = $jSON.Operation
ObjectId = $jSON.ObjectId
}

} | Select-object -Property @(
@{Name = 'User'; Expression = 'UserId' }
@{Name = 'Date & Time'; Expression = 'CreationTime' }
@{Name = 'Type of Action'; Expression = 'Operation' }
@{Name = "Criteria"; Expression = "RecordType" }
@{Name = "Item Search"; Expression = "ObjectId" }
@{Name = "Result Status"; Expression = "ResultStatus" }
) | Export-Csv $modifiedFile -NoTypeInformation

这就是 UTC 时间现在的样子。

enter image description here

2021-12-01T18:23:21
2021-12-01T18:23:21
2021-12-01T18:23:21

最佳答案

使用TimeZoneInfo.ConvertTime() :

# sample UTC datetime value
$dt = [datetime]::UtcNow

# define target time zone
$targettz = [System.TimeZoneInfo]::FindSystemTimeZoneById('Eastern Standard Time')

# convert to target timezone
[System.TimeZoneInfo]::ConvertTime($dt, $targettz)

这适用于本地化和通用日期时间值。

如果您希望它在遇到本地化值时失败,请使用 ConvertTimeFromUtc():

# this will still work
$dt = [datetime]::UtcNow
[System.TimeZoneInfo]::ConvertTimeFromUtc($dt, $targettz)

# this will fail
$dt = [datetime]::Now
[System.TimeZoneInfo]::ConvertTimeFromUtc($dt, $targettz)

关于powershell - 如何使用 PowerShell 将 UTC 转换为 EST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70204020/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com