'Windows ISO 8601 timestamp

I need to convert a date in Windows PowerShell to the ISO 8601 format.

In Linux/Unix it was no problem with

TZ=0 date -d "<random-format>" +%Y-%m-%dT%H:%M:%S.000Z

Now I need to do the same in Windows PowerShell. The output format on Windows is

Wednesday, 19. July 2017 01:06:13

How can I do it?



Solution 1:[1]

PowerShell's Get-Date supports standard .NET time formats. The o round-trip format complies with ISO 8601. Like so,

Get-Date -Format "o"

2017-08-15T12:10:34.4443084+03:00

Solution 2:[2]

Get-Date supports Unix formatting strings with the -UFormat parameter. You can reuse it:

Get-Date (Get-Date).ToUniversalTime() -UFormat '+%Y-%m-%dT%H:%M:%S.000Z'

Solution 3:[3]

The following works both in Windows PowerShell 5.1 and PowerShell 7.0 on Windows/OS X:

 (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffK")
 
 2020-12-01T22:31:41.402Z

If you don't want your milliseconds, then format string would be "yyyy-MM-ddTHH:mm:ss.000K"

Solution 4:[4]

Old question but since none of the other answers has it, if you're looking for UTC, this seems to do it:

(Get-Date).ToUniversalTime().ToString("o")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2
Solution 3 sokhaty
Solution 4 rom99