Powershell: Get-Date: format option for ISO 8601 in UTC

Created on 20 Dec 2019  路  4Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

Get-Date should support ISO 8601 to return in UTC. Currently the -format s option returns in ISO 8601 but local time.
There should be a new option which formats including the timezone.

e.g. instead of 2019-12-20T08:48:05 in UTC +1, it should be 2019-12-20T07:48:05Z

https://tools.ietf.org/html/rfc3339#section-5.8

Proposed technical implementation details (optional)

Area-Cmdlets-Utility First-Time-Issue Issue-Enhancement Resolution-Fixed Up-for-Grabs

Most helpful comment

S is a short cut for "SortableDateTimePattern" which is defined in the current culture.
It is equivalent to adding .tostring((Get-Culture).DateTimeFormat.SortableDateTimePattern)

The .Net formatter seems to do some odd things. e.g. [datetime]::now.tostring("r") Will append "GMT" to the converted date time regardless of the timezone it came from

[datetime]::now.tostring("HH:mm zzz") returns the offset information, e.g. _13:25 +01:00_

But the formatter doesn't seem to have a method for converting local time to universal time or vice versa. It needs to be done before the formatting is applied.
[datetime]::now.ToUniversalTime().tostring("HH:mm zzz")

Ideally Get-Date would have -UniversalTime as a switch.

All 4 comments

S is a short cut for "SortableDateTimePattern" which is defined in the current culture.
It is equivalent to adding .tostring((Get-Culture).DateTimeFormat.SortableDateTimePattern)

The .Net formatter seems to do some odd things. e.g. [datetime]::now.tostring("r") Will append "GMT" to the converted date time regardless of the timezone it came from

[datetime]::now.tostring("HH:mm zzz") returns the offset information, e.g. _13:25 +01:00_

But the formatter doesn't seem to have a method for converting local time to universal time or vice versa. It needs to be done before the formatting is applied.
[datetime]::now.ToUniversalTime().tostring("HH:mm zzz")

Ideally Get-Date would have -UniversalTime as a switch.

There is Get-Date -Format FileDateTimeUniversal.

The switch could be -AsUTC.

I'd be happy to take this one on!

awesome! thank you @brendandburns!

Was this page helpful?
0 / 5 - 0 ratings