get a list of powershell date format outputs

I want to create a list of Powershell date formats and their outputs, for a quick reference doc.

I’ve collected a list of formats from the Powershell help text and other locations and put them in a big text file which looks like this:

get-date -Uformat %A   # Day of the week - full name 
get-date -Uformat %u   # Day of the week - number (Monday = 1)
get-date -Uformat %d   # Day of the month - 2 digits 
get-date -Uformat %e   # Day of the month - digit preceded by a space ( 5)
get-date -Uformat %j   # Day of the year 
get-date -Uformat %p   # AM or PM
get-date -Uformat %r   # Time in 12-hour format
get-date -Uformat %R   # Time in 24-hour format - no seconds
get-date -Uformat %T   # Time in 24 hour format

I’m going to order this in what I consider to be order of usefullness (to me), because I’m going to plug it into an about_dateformats help page in my Powershell-help-powered repository of notes and tips.

To generate a list with the command, output and comment, I did this:

$Dates = foreach ($L in $(Select-String -notmatch "^$" formats.txt)) 
{
  [string]$Line = $L.Line
  $Command = $Line.split('#')[0]
  $Comment = $Line.split('#')[1]

  $scriptBlock = $executioncontext.InvokeCommand.NewScriptBlock($Line)

  $Output = invoke-command $ScriptBlock

  # write-output "$Command $Output # $Comment"
  new-object PSObject -Property @{
     Command = $Command
     Output =  $Output
     Comment = $Comment
  }

} 

$Dates | ft -a

The output looks like this:

<br />Output                            Command                                             Comment                                                           
------                            -------                                             -------                                                           
10                                Get-Date -UFormat %d                                                                                                  
Wed Feb 10 12:44:52 2016          get-date -UFormat %c                                 Date and time - abbreviated (Fri Jun 16 10:31:27 2006)           
02/10/16                          get-date -UFormat %D                                 Date in mm/dd/yy format (06/14/06)                               
02/10/16                          get-date -UFormat %x                                 Date in standard format for locale (09/12/07 for English-US)     
20                                get-date -Uformat %C                                 Century (20 for 2006)                                            
2016                              get-date -Uformat %Y                                 Year in 4-digit format (2006)                                    
16                                get-date -Uformat %y                                 Year in 2-digit format (06)                                      
Feb                               get-date -Uformat %b                                 Month name - abbreviated (Jan)                                   
February                          get-date -Uformat %B                                 Month name - full (January)                                      
02                                get-date -Uformat %m                                 Month number (06)                                                
5                                 get-date -Uformat %W                                 Week of the year (00-52)                                         
6                                 get-date -Uformat %V                                 Week of the year (01-53)                                         
Wed                               get-date -Uformat %a                                 Day of the week - abbreviated name (Mon)                         
Wednesday                         get-date -Uformat %A                                 Day of the week - full name (Monday)                             
3                                 get-date -Uformat %u                                 Day of the week - number (Monday = 1)                            
10                                get-date -Uformat %d                                 Day of the month - 2 digits (05)                                 
10                                get-date -Uformat %e                                 Day of the month - digit preceded by a space ( 5)                
41                                get-date -Uformat %j                                 Day of the year - (1-366)                                        
PM                                get-date -Uformat %p                                 AM or PM                                                         
12:44:52 PM                       get-date -Uformat %r                                 Time in 12-hour format (09:15:36 AM)                             
12:44                             get-date -Uformat %R                                 Time in 24-hour format - no seconds (17:45)                      
12:44:52                          get-date -Uformat %T                                 Time in 24 hour format (17:45:52)                                
+00                               get-date -Uformat %Z                                 Time zone offset from Universal Time Coordinate (UTC) (-07)      
12                                get-date -Uformat %H                                 Hour in 24-hour format (17)                                      
12                                get-date -Uformat %I                                 Hour in 12 hour format (05)                                      
44                                get-date -Uformat %M                                 Minutes (35)                                                     
52                                get-date -Uformat %S                                 Seconds (05)                                                     
1455108292.3719                   get-date -Uformat %s                                 Seconds elapsed since January 1, 1970 00:00:00 (1150451174.95705)
10/02/2016 12:44:52               Get-Date -DisplayHint Date                                                                                            
10/02/2016 12:44                  Get-Date -Format g                                                                                                    
2016 / 02 / 10 / Wednesday / +00  Get-Date -UFormat "%Y / %m / %d / %A / %Z"                                                                            
366                               (Get-Date -Year 2000 -Month 12 -Day 31).DayOfYear                                                                     
False                             $(get-date).IsDaylightSavingTime()                                                                                    
10/02/2016 12:44:52               $(get-date).ToUniversalTime()                                                                                         
10/02/2016 12:44:52               (Get-Date).ToString()                                                                                                 
2016-02-10T12:44:52.3749035+00:00 Get-Date -Format o                                                                                                    
2016-02-10T12.44.52.3759036+00.00 Get-Date -Format o | foreach {$_ -replace ":", "."}                           
Advertisements