PowerShell 中是否有一种巧妙的方法来确定另一个时区的过去日期是否是夏令时?情况是这样的,我们的数据库在欧洲,时间和日期是该服务器的本地时间。由于欧洲和美国在不同时间开始和停止 DST,因此我需要考虑这些时间的时差。 感谢您的建议。
最佳答案
无需尝试确定 DST 是否生效。让 .NET Framework 为您完成转换。
首先,确定要转换的时区 ID。
你说你的输入时间是在欧洲,但你没有具体说明是哪里。喜欢美国,Europe has multiple time zones .我假设您指的是 CET/CEST (UTC+1/+2)。在 Windows 中,您可以为此使用以下任何标识符:
“中欧标准时间”
“欧洲中部标准时间”
“浪漫标准时间”
(为什么?谁知道呢。)“W. Europe Standard Time”
(不要被名字骗了,它仍然是中央时间)
除了它们的显示名称外,这些都是相同的(在 Windows 中)。如果您想准确选择哪一个,请参阅 the CLDR reference .
对于美国中部时区,使用“Central Standard Time”
。
然后只需使用 TimeZoneInfo.ConvertTimeBySystemTimeZoneId
函数,如下:
# collect your input (through db, or whatever mechanism you have)
$inputDateTime = Get-Date -Date "2017-06-27 00:00:00"
# pick your time zones
$fromTimeZone = "Central Europe Standard Time" # EU
$toTimeZone = "Central Standard Time" # USA
# convert
$outputDateTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
$inputDateTime, $fromTimeZone, $toTimeZone)
# output
Write-Output $outputDateTime
关于powershell - 确定 Powershell 中不同时区的夏令时状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781312/