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?
Briefly, the ISO 8601 notation consists of a P character, followed by years, months, weeks, and days, followed by a T character, followed by hours, minutes, and seconds with a decimal part, each with a single-letter suffix that indicates the unit. Any zero components may be omitted.
The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .
Time zones in ISO 8601 are represented as local time (with the location unspecified), as UTC, or as an offset from UTC.
ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).
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
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'
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