Using Get-Date -Format is beyond the scope of this blog. It’s covered very well in Microsoft’s official docs. I’m just going to discuss the single character shortcuts that aren’t well-documented.
Quick Usage
1 2 |
PS > Get-Date -Format 'O' 2020-10-16T23:08:26.8758919-05:00 |
Other Single Letter Formats
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
D: Friday, October 16, 2020 F: Friday, October 16, 2020 11:08:26 PM G: 10/16/2020 11:08:26 PM M: October 16 O: 2020-10-16T23:08:26.8758919-05:00 R: Fri, 16 Oct 2020 23:08:26 GMT T: 11:08:26 PM U: Saturday, October 17, 2020 4:08:26 AM Y: October 2020 d: 10/16/2020 f: Friday, October 16, 2020 11:08 PM g: 10/16/2020 11:08 PM m: October 16 o: 2020-10-16T23:08:26.9703682-05:00 r: Fri, 16 Oct 2020 23:08:26 GMT s: 2020-10-16T23:08:26 t: 11:08 PM u: 2020-10-16 23:08:26Z y: October 2020 |
Figuring Out the Get-Date -Format
Options
I’ve known for a while that Get-Date -Format 'O' gave me a super simple timestamp. I use it a lot for timestamping files names, like logs. Of course I have to replace the semicolons (Get-Date -Format 'O').Replace(':', '-'). Anyway, today, I was wondering what the other options were, so I took a trick out of password generating to quickly go through all of the upper and lowercase characters as options.
Since I know that ascii characters can be represented by numbers, and I know that A through Z is 65 to 90 and a through z is 97 to 122, I just decided to make a quick loop to spit out all the date options. See and try for yourself.
1 2 3 4 5 |
foreach ($i in (65..90 + 97..122)) { [char] $l = $i Write-Host "${l}: " -ForegroundColor Cyan -NoNewLine Get-Date -Format $l } |
One-Liner in the CLI
Who wants to type all that in the CLI though? Here’s the one-liner with the respective output to save you the time:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
PS C:\> 65..90+97..122 | %{[char]$l=$_; Write-Host "${l}: " -F Cy -N; Get-Date -F $l } A: Get-Date: Input string was not in a correct format. B: Get-Date: Input string was not in a correct format. C: Get-Date: Input string was not in a correct format. D: Friday, October 16, 2020 E: Get-Date: Input string was not in a correct format. F: Friday, October 16, 2020 11:36:11 PM G: 10/16/2020 11:36:11 PM H: Get-Date: Input string was not in a correct format. I: Get-Date: Input string was not in a correct format. J: Get-Date: Input string was not in a correct format. K: Get-Date: Input string was not in a correct format. L: Get-Date: Input string was not in a correct format. M: October 16 N: Get-Date: Input string was not in a correct format. O: 2020-10-16T23:36:11.6121086-05:00 P: Get-Date: Input string was not in a correct format. Q: Get-Date: Input string was not in a correct format. R: Fri, 16 Oct 2020 23:36:11 GMT S: Get-Date: Input string was not in a correct format. T: 11:36:11 PM U: Saturday, October 17, 2020 4:36:11 AM V: Get-Date: Input string was not in a correct format. W: Get-Date: Input string was not in a correct format. X: Get-Date: Input string was not in a correct format. Y: October 2020 Z: Get-Date: Input string was not in a correct format. a: Get-Date: Input string was not in a correct format. b: Get-Date: Input string was not in a correct format. c: Get-Date: Input string was not in a correct format. d: 10/16/2020 e: Get-Date: Input string was not in a correct format. f: Friday, October 16, 2020 11:36 PM g: 10/16/2020 11:36 PM h: Get-Date: Input string was not in a correct format. i: Get-Date: Input string was not in a correct format. j: Get-Date: Input string was not in a correct format. k: Get-Date: Input string was not in a correct format. l: Get-Date: Input string was not in a correct format. m: October 16 n: Get-Date: Input string was not in a correct format. o: 2020-10-16T23:36:11.6898769-05:00 p: Get-Date: Input string was not in a correct format. q: Get-Date: Input string was not in a correct format. r: Fri, 16 Oct 2020 23:36:11 GMT s: 2020-10-16T23:36:11 t: 11:36 PM u: 2020-10-16 23:36:11Z v: Get-Date: Input string was not in a correct format. w: Get-Date: Input string was not in a correct format. x: Get-Date: Input string was not in a correct format. y: October 2020 z: Get-Date: Input string was not in a correct format. |
Fixing Zulu
If you’re working with Zulu (aka GMT) time, the formatter doesn’t actually convert the timestamp to Zulu. You will have to do that yourself. Here’s without the fix and with the fix:
1 2 3 4 |
PS > Get-Date -Format 'u' 2020-10-17 00:31:08Z PS > Get-Date (Get-Date).AddHours(-(Get-Date -Format '%z')) -Format 'u' 2020-10-17 05:31:08Z |