Title: Add Four New Special Formats to Get-Date · Issue #11714 · PowerShell/PowerShell · GitHub
Open Graph Title: Add Four New Special Formats to Get-Date · Issue #11714 · PowerShell/PowerShell
X Title: Add Four New Special Formats to Get-Date · Issue #11714 · PowerShell/PowerShell
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...
Open Graph 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...
X 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...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/11714
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add Four New Special Formats to Get-Date","articleBody":"I happened to notice that a recent code change (https://github.com/PowerShell/PowerShell/pull/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.\r\n\r\nLooking at src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs\r\nwe already have four special formats that were introduced in PS5 \"FileDate\", \"FileDateUniversal\", \"FileDateTime\", and \"FileDateTimeUniversal\"\r\n\r\n```\r\nGet-Date -Format \"FileDate\" # yyyyMMdd\r\n# which is equivalent to Get-Date -Format \"yyyyMMdd\" or (Get-Date -UFormat \"%F\") -Replace '-',''\r\nGet-Date -Format \"FileDateUniversal\" # yyyyMMddZ\r\n#which is equivalent to Get-Date -AsUTC -Format \"yyyyMMdd'Z'\" or (Get-Date -UFormat \"%FZ\") -Replace '-',''\r\nGet-Date -Format \"FileDateTime\" # yyyyMMddTHHmmssffff\r\n# which is equivalent to Get-Date -Format \"yyyyMMdd'T'HHmmssffff\"\r\nGet-Date -Format \"FileDateTimeUniversal\" # yyyyMMddTHHmmssffffZ\r\n# which is equivalent to Get-Date -AsUTC -Format \"yyyyMMdd'T'HHmmssffff'Z'\"\r\n```\r\n\r\nMy 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).\r\nIf you use many REST APIs, or JSON you may find these come up quite frequently.\r\n\r\n```\r\nGet-Date -Format \"SecondsSinceEpoch\" # (in UTC)\r\nGet-Date -Format RFC3339datetime\r\n# equates to Get-Date -Format o or ([Newtonsoft.Json.JsonConvert]::SerializeObject($(Get-Date -AsUTC))) -Replace '\"',''\r\nGet-Date -Format RFC3339fulldate\r\n# equates to Get-Date -Format \"yyyy-MM-dd\"\r\nGet-Date -Format RFC3339fulltime\r\n# equates to Get-Date -Format \"HH:mm:ss.fffffffK\"\r\n```\r\n\r\nCould these value be gotten via other means? Sure! Just as the existing special formats could be.\r\n\r\nI currently use these two functions to get back and forth with the UnixEpoch.\r\n\r\n```\r\nfunction Convert-DateTimeToUnixTime([DateTime]$DateTimeVar) {\r\n # Output is UTC.\r\n if ($DateTimeVar.Kind -ne \"Utc\") { $DateTimeVar = $DateTimeVar.ToUniversalTime() }\r\n $unixEpochStart = New-Object DateTime (1970, 1, 1, 0, 0, 0, ([DateTimeKind]::Utc))\r\n [int]($DateTimeVar - $unixEpochStart).TotalSeconds\r\n}\r\n\r\nfunction Convert-UnixTimeToDateTime([int]$UnixTime) {\r\n # Output is UTC. ToLocalTime can be used after the fact or added in as a switch.\r\n (New-Object DateTime(1970, 1, 1, 0, 0, 0, 0, [DateTimeKind]::Utc)).AddSeconds($UnixTime)\r\n}\r\n```\r\n\r\n\r\nAnyway, I thought I would propose this and see what others thought.\r\n\r\nBest!\r\n","author":{"url":"https://github.com/scotthardwick","@type":"Person","name":"scotthardwick"},"datePublished":"2020-01-29T00:00:53Z","url":"https://github.com/PowerShell/PowerShell/issues/11714"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b0a530b2-b594-d7b3-9b3b-31fa576f1594 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E1FC:2F1BC5:238D89:30B746:6AFD5213 |
| html-safe-nonce | 1bb2723d5af070b5364eda8b4137a84452936b3c60b7615d9c4529c7971a4802 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMUZDOjJGMUJDNToyMzhEODk6MzBCNzQ2OjZBRkQ1MjEzIiwidmlzaXRvcl9pZCI6IjE1NzcyMDQxNzU3NjkyNjA1NjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 12362e638dbd37a4088daa0df54add38b250dd620d962bf68e274a37fea8f73c |
| hovercard-subject-tag | issue:556560441 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/11714/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6375e00d97457566b871307036d6dfa2d21ce0e673a3a15cbd39979a7e53d2cb/PowerShell/PowerShell/issues/11714 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6375e00d97457566b871307036d6dfa2d21ce0e673a3a15cbd39979a7e53d2cb/PowerShell/PowerShell/issues/11714 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | scotthardwick |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width