Repository navigation
Description
I happened to notice that a recent code change (#11611) has added the -AsUTC switch to the Get-Date command and that got me to thinking about checking out how this was going to really simplify life. I then started wondering what else Get-Date needed to be highly useful. So I revisited the Get-Date code and various projects I have done over the last few years.
Looking at src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs
we already have four special formats that were introduced in PS5 "FileDate", "FileDateUniversal", "FileDateTime", and "FileDateTimeUniversal"
Get-Date -Format "FileDate" # yyyyMMdd
# which is equivalent to Get-Date -Format "yyyyMMdd" or (Get-Date -UFormat "%F") -Replace '-',''
Get-Date -Format "FileDateUniversal" # yyyyMMddZ
#which is equivalent to Get-Date -AsUTC -Format "yyyyMMdd'Z'" or (Get-Date -UFormat "%FZ") -Replace '-',''
Get-Date -Format "FileDateTime" # yyyyMMddTHHmmssffff
# which is equivalent to Get-Date -Format "yyyyMMdd'T'HHmmssffff"
Get-Date -Format "FileDateTimeUniversal" # yyyyMMddTHHmmssffffZ
# which is equivalent to Get-Date -AsUTC -Format "yyyyMMdd'T'HHmmssffff'Z'"
My suggestion is that four equally useful special formats that should be added are SecondsSinceEpoch (or UnixTime or UnixEpoch or whatever is the most appropriate label) and ISO8601 (or the profile RFC3339 as a label may be more apt).
If you use many REST APIs, or JSON you may find these come up quite frequently.
Get-Date -Format "SecondsSinceEpoch" # (in UTC)
Get-Date -Format RFC3339datetime
# equates to Get-Date -Format o or ([Newtonsoft.Json.JsonConvert]::SerializeObject($(Get-Date -AsUTC))) -Replace '"',''
Get-Date -Format RFC3339fulldate
# equates to Get-Date -Format "yyyy-MM-dd"
Get-Date -Format RFC3339fulltime
# equates to Get-Date -Format "HH:mm:ss.fffffffK"
Could these value be gotten via other means? Sure! Just as the existing special formats could be.
I currently use these two functions to get back and forth with the UnixEpoch.
function Convert-DateTimeToUnixTime([DateTime]$DateTimeVar) {
# Output is UTC.
if ($DateTimeVar.Kind -ne "Utc") { $DateTimeVar = $DateTimeVar.ToUniversalTime() }
$unixEpochStart = New-Object DateTime (1970, 1, 1, 0, 0, 0, ([DateTimeKind]::Utc))
[int]($DateTimeVar - $unixEpochStart).TotalSeconds
}
function Convert-UnixTimeToDateTime([int]$UnixTime) {
# Output is UTC. ToLocalTime can be used after the fact or added in as a switch.
(New-Object DateTime(1970, 1, 1, 0, 0, 0, 0, [DateTimeKind]::Utc)).AddSeconds($UnixTime)
}
Anyway, I thought I would propose this and see what others thought.
Best!
Activity
[-]Add Two New Special Formats to Get-Date and One New DateTime Parsing Format[/-][+]Add Four New Special Formats to Get-Date and One New DateTime Parsing Format[/+][-]Add Four New Special Formats to Get-Date and One New DateTime Parsing Format[/-][+]Add Two New Special Formats to Get-Date and One New DateTime Parsing Format[/+][-]Add Two New Special Formats to Get-Date and One New DateTime Parsing Format[/-][+]Add Four New Special Formats to Get-Date and One New DateTime Parsing Format[/+]iSazonov commentedon Jan 29, 2020
Scott Hardwick (@scotthardwick) Please move second proposal in new issue.
[-]Add Four New Special Formats to Get-Date and One New DateTime Parsing Format[/-][+]Add Four New Special Formats to Get-Date[/+]scotthardwick commentedon Jan 29, 2020
Ilya (@iSazonov) I have done as you requested and separated the two proposals.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.