获取格式化的通用日期/时间
Get formatted universal date / time
在 PowerShell 中,您可以将日期格式化为 return 当前小时,如下所示:
Get-Date -UFormat %H
你可以像这样获得 UTC 日期字符串:
$dateNow = Get-Date
$dateNow.ToUniversalTime()
但是如何才能得到世界时的当前小时呢?
Get-Date ([datetime]::UtcNow) -UFormat %H
直到 PowerShell 7.0,Get-Date
不直接支持根据 UTC 值格式化当前时间:格式选项适用于当前时间的 local 表示。
- 在 PowerShell v7.1+ 中,您可以使用
-AsUTC
开关,这使您可以简化为
Get-Date -AsUTC -UFormat %H
.
但是,如上所示,您可以传递一个 [datetime]
实例作为 参数 [=34],而不是让 Get-Date
默认为当前(总是本地)时间=] 到 Get-Date
(位置隐含的 -Date
参数)进行重新格式化,而 [datetime]::UtcNow
是以 UTC 表示的当前时间。
在 PowerShell 中,您可以将日期格式化为 return 当前小时,如下所示:
Get-Date -UFormat %H
你可以像这样获得 UTC 日期字符串:
$dateNow = Get-Date
$dateNow.ToUniversalTime()
但是如何才能得到世界时的当前小时呢?
Get-Date ([datetime]::UtcNow) -UFormat %H
直到 PowerShell 7.0,Get-Date
不直接支持根据 UTC 值格式化当前时间:格式选项适用于当前时间的 local 表示。
- 在 PowerShell v7.1+ 中,您可以使用
-AsUTC
开关,这使您可以简化为
Get-Date -AsUTC -UFormat %H
.
但是,如上所示,您可以传递一个 [datetime]
实例作为 参数 [=34],而不是让 Get-Date
默认为当前(总是本地)时间=] 到 Get-Date
(位置隐含的 -Date
参数)进行重新格式化,而 [datetime]::UtcNow
是以 UTC 表示的当前时间。