在 PowerShell 中,您可以格式化日期以返回当前小时,如下所示:
Get-Date -UFormat %H
您可以像这样在 UTC 中获取日期字符串:
$dateNow = Get-Date
$dateNow.ToUniversalTime()
但是你怎么能得到世界时的当前小时呢?
最佳答案
Get-Date ([datetime]::UtcNow) -UFormat %H
最高 PowerShell 7.0 , Get-Date
不直接支持根据其 UTC 值格式化当前时间 :格式化选项适用于当前时间的本地表示。-AsUTC
开关,这使您能够简化到Get-Date -AsUTC -UFormat %H
. 但是,如上图所示,而不是让
Get-Date
默认为当前(总是本地)时间,您可以传递 [datetime]
实例作为 Get-Date
的参数(位置隐含的 -Date
参数)用于重新格式化,以及 [datetime]::UtcNow
是以 UTC 表示的当前时间。
关于powershell - 获取格式化的通用日期/时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40244228/