如何将Powershell UTC日期时间对象转换为EST

我输入了日期时间字符串,其格式如下:

let productId = 12; if (true) { let productId = 10 console.log(this.productId) // results in 'undefined' console.log(this) //results in '{}' console.log(productId) // results in '10' }

我需要能够将它们转换为EST。我尝试过的每个函数都会引发一个或另一个错误,通常是:

2017-08-03T12:30:00.000Z

我尝试了以下变化:

"String was not recognized as a valid DateTime."

我认为这与我引用的日期时间字符串如何与$time = '2017-08-03T12:30:00.000Z' [datetime]$datetime = $time $culture = [Globalization.CultureInfo]::invariantculture [DateTime]::ParseExact($datetime,'MM-dd-yyyy HH:mm:ss',$culture) 和UTC时间有关,但无法弄清楚该如何处理。也许我应该解析时间,将其转换,然后重新附加到字符串的第一部分,即日期,并将它们组合在一起以得到最终输出?似乎工作量太大,并且解决方案将来可能会导致潜在的错误。

qqgz0nimab 回答:如何将Powershell UTC日期时间对象转换为EST

您应该只需将其转换即可将Zulu时间字符串转换为DateTime值。但是,结果值将以当地时间为准,因此您应将其转换回UTC以进行进一步的计算:

$timestamp = '2017-08-03T12:30:00.000Z'
$datetime  = ([DateTime]$timestamp).ToUniversalTime()

然后,您可以使用TimeZoneInfo类将UTC时间戳转换为所需的时区:

[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($datetime,'Eastern Standard Time')

使用[TimeZoneInfo]::GetSystemTimeZones() | Select-Object Id,DisplayName获取可识别的时区列表。

,

尝试使用ConvertTimeBySystemTimeZoneId()类的静态[System.TimeZoneInfo]方法:

$time = '2017-08-03T12:30:00.000Z'
$result = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date -Date $time),'Eastern Standard Time')

返回的$ result是[DateTime]

顺便说一句,如果您需要将其转换回去,请按以下步骤操作:

Get-Date -Date $result -Format FileDateTimeUniversal

希望这会有所帮助。

本文链接:https://www.f2er.com/3123637.html

大家都在问