Skip to content

Add Four New Special Formats to Get-Date #11714

Closed

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

  1. added
    Issue-Enhancement
    on Jan 29, 2020
  2. changed the title [-]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[/+] on Jan 29, 2020
  3. changed the title [-]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[/+] on Jan 29, 2020
  4. changed the title [-]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[/+] on Jan 29, 2020
  5. iSazonov commented on Jan 29, 2020

    @iSazonov
    Collaborator

    Scott Hardwick (@scotthardwick) Please move second proposal in new issue.

  6. changed the title [-]Add Four New Special Formats to Get-Date and One New DateTime Parsing Format[/-] [+]Add Four New Special Formats to Get-Date[/+] on Jan 29, 2020
  7. scotthardwick commented on Jan 29, 2020

    @scotthardwick
    Author

    Ilya (@iSazonov) I have done as you requested and separated the two proposals.

  8. added
    Issue-Discussion
    on Jan 30, 2020
  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  12. added
    Resolution-No Activity
    on Nov 16, 2023
  13. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionIssue-EnhancementResolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions