Get-Date.ToString('r') 返回不正确的时间(GMT 而不是本地时区)

g18*_*18c 3 powershell

PowerShell 没有正确显示时间并且忽略了我的本地时区 - 目前是 00:20(格林威治标准时间 +2),但是在获取时间时显示本地时间 (00:20) 但错误地打印了格林威治标准时间。

检查运行脚本的服务器,以及时区和本地时间设置是否正确。

我做错了什么还是这是一个错误?

$scriptStartDate = Get-Date
Write-Host "time is $($scriptStartDate.ToString('r'))"
# above line prints 'Sun, 14 Apr 2013 00:20:00 GMT'
# this is wrong as wrong timezone, it should be 'Sun, 14 Apr 2013 00:20:00 E.Europe'
Run Code Online (Sandbox Code Playgroud)

Kei*_*ill 5

由您来确保时间是 UTC。你可以这样做:

[DateTime]::UtcNow.ToString('r')
Run Code Online (Sandbox Code Playgroud)

这种格式的文档说明如下:

格式化不会修改 DateTime 的值;因此,您必须在格式化之前将值调整为 GMT。