In PowerShell you can format a date to return the current hour like this:
Get-Date -UFormat %H
And you can get a date string in UTC like this:
$dateNow = Get-Date
$dateNow.ToUniversalTime()
But how can you can get the current hour in universal time?
Get-Date ([datetime]::UtcNow) -UFormat %H
Up to PowerShell 7.0, Get-Date
doesn't directly support formatting the current time based on its UTC value: the formatting options apply to the local representation of the current time.
-AsUTC
switch, which enable you to simplify toGet-Date -AsUTC -UFormat %H
.However, as shown above, instead of letting Get-Date
default to the current (invariably local) time, you can pass a [datetime]
instance as an argument to Get-Date
('s positionally implied -Date
parameter) for reformatting, and [datetime]::UtcNow
is the current time expressed in UTC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With