Skip to content

Add Four New Special Formats to Get-Date #11714

@scotthardwick

Description

@scotthardwick

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions