使用纯powershell脚本将任意时区时间转换成东八区(GMT+08:00)时间

如题,去网上找了半天没找到,然后之前自己还死脑筋地没转过弯来,写了一大堆错误逻辑……

注释都写的很明白了,不再赘述

# Which time zone you need
$NeedZone = [Int16]8
# Which format you'd like to use for output
$OutputFormat = "%Y-%m-%d_%H:%M:%S"
#
#
# Get time now as seconds with zone
$NowTime = [UInt32](Get-Date -UFormat "%s")
# Get zone as int (never use uint for this)
$Zone = [Int16](Get-Date -UFormat "%Z").ToString()
# Switch to Zone you need
$NeedTime = ($NeedZone - $Zone) * 60 * 60
$Result = $NowTime + $NeedTime
# Format it
$Formated = (Get-Date -UnixTimeSeconds $Result -UFormat $OutputFormat).ToString()

$Formated