基本上,我正在尝试找到一种将特定时区的日期时间转换为其他时区的方法,同时也将DST考虑在内。例如
例如,“东京标准时间”的“中太平洋标准时间”是2012/9/29 9:00 AM?
我在Internet上找到了一些解决方案,可以将本地计算机时间转换为其他时区。
$ToTimeZoneObj = [system.timezoneinfo]::GetSystemTimeZones() | Where-Object {$_.id -eq $ToTimeZone}
$TargetZoneTime = [system.timezoneinfo]::ConvertTime($datetime, $ToTimeZoneObj)
我在考虑是否可以创建与本地计算机不同的时区的datetime对象,然后可以使用找到的解决方案,或者是否有其他方法可以满足需要?
谢谢
最佳答案
此解决方案对我来说效果很好:
$cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
$csttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $cstzone)
然后,您可以像处理datetime对象一样操作$ csttime变量:
Get-Date $csttime.AddHours(-1) -f "MM\dd\yyyy HH:mm:ss"
引用文献:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimefromutc(v=vs.110).aspx
http://technet.microsoft.com/en-us/library/ee692801.aspx
关于datetime - 在Powershell中创建其他时区的datetime对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12649055/