powershell - 确定 Powershell 中不同时区的夏令时状态

标签 powershell datetime timezone

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/

相关文章:

powershell - 在Powershell中难以从文本文件中提取响应

python - 如何存储像 "The last day in Feb"、 "Fourth Saturday in April"这样的描述性日期?

mysql - 如何更改在virtualbox中的linux mint上运行的mysql时区?

具有不同时区的 MySQL 日期时间

powershell - 如何使用 Powershell 读取登录事件并查找用户信息?

windows - Powershell ConvertTo-SecureString ObjectNotFound

wpf - 基于 DateTime 创建自定义 GroupDescription

python - 如何获取与当前时区对应的 tz_info 对象?

powershell - 管道循环到 out-gridview 并在每个循环上清除网格?

MySQL:将日期时间插入其他日期时间字段