Powershell's easiest way to get the current time, expressed as UTC

I looked at the message Creating a DateTime object with a specific UTC DateTime in PowerShell , but it does not seem to directly answer the question I ask:

What is the most direct method in PowerShell (3.0) to return a sortable string representing "now" as UTC?

I expected the correct answer would be:

Get-Date -Format (Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern 

 OR

get-date -format u

but this is not so.

Example: at 13:00 (1 hour) on September 1, 2016 in the Pacific time zone during DST, I get the answer:

2016-09-01 13: 00: 00Z (local time with the addition of "Z")

when i was expecting:

2016-09-01 20: 00: 00Z (correct time UTC / GMT)

So basically, my code just gets a line representing "local" time, and adds "Z".

, , , ( , ) .

( ): , , "UTC" / "GMT" . .

+4
1

, - :

[DateTime]::UtcNow.ToString('u')

:

[DateTime]::UtcNow.ToString((Get-Culture).DateTimeFormat.UniversalSortableDateTimePattern)

, - Z UTC:

[DateTime]::UtcNow.ToString('u').Replace('Z','UTC')

, UTC, . , , 3- .

+16

All Articles