datetime - 在Powershell中创建其他时区的datetime对象

标签 datetime powershell timezone

基本上,我正在尝试找到一种将特定时区的日期时间转换为其他时区的方法,同时也将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/

相关文章:

json - 在 CouchDB 中存储日期时间(时间戳)的最佳方式是什么?

powershell - 从脚本中的 powershell 命令获取退出代码并将其传递给 Jenkins

powershell - Powershell启动服务失败

python - 显示 : character in the timezone offset using datetime. strftime

node.js - 如何在 Node js 中为 windows 设置默认时区

iPhone 和 JSON : Encode dates in JSON objects for use in iPhone applications

python - 如何计算两个给定日期之间的天数

java - Joda 中的“负”持续时间,是错误还是功能?

带有可选参数的 powershell 调用方法

java - 比较 Joda-Time 时区