One-Liner: GetDateTimeFormats
DateTime objects are amazing. You can format them in so many different ways, but they also come with a boatload formats pre-defined. Oh, and it uses the current culture. So much cleaner. But which is which?
$i
= 0; (Get-Date).GetDateTimeFormats() | Select-Object @{name = 'index'; expression = {$i; $null = $global:i++ }}, @{name = 'output'; expression = {$_;}}
This also uses one of my favourite tricks recently - synthetic properties from Select-Object, instead of using the rigamarole that is New-Object and then Add-Member.
Anyhow, here is the output (for my culture, that is - your mileage may vary):
index output
----- ------
0 5/15/2010
1 5/15/10
2 05/15/10
3 05/15/2010
4 10/05/15
5 2010-05-15
6 15-May-10
7 Saturday, May 15, 2010
8 May 15, 2010
9 Saturday, 15 May, 2010
10 15 May, 2010
11 Saturday, May 15, 2010 9:09 AM
12 Saturday, May 15, 2010 09:09 AM
13 Saturday, May 15, 2010 9:09
14 Saturday, May 15, 2010 09:09
15 May 15, 2010 9:09 AM
16 May 15, 2010 09:09 AM
17 May 15, 2010 9:09
18 May 15, 2010 09:09
19 Saturday, 15 May, 2010 9:09 AM
20 Saturday, 15 May, 2010 09:09 AM
21 Saturday, 15 May, 2010 9:09
22 Saturday, 15 May, 2010 09:09
23 15 May, 2010 9:09 AM
24 15 May, 2010 09:09 AM
25 15 May, 2010 9:09
26 15 May, 2010 09:09
27 Saturday, May 15, 2010 9:09:59 AM
28 Saturday, May 15, 2010 09:09:59 AM
29 Saturday, May 15, 2010 9:09:59
30 Saturday, May 15, 2010 09:09:59
31 May 15, 2010 9:09:59 AM
32 May 15, 2010 09:09:59 AM
33 May 15, 2010 9:09:59
34 May 15, 2010 09:09:59
35 Saturday, 15 May, 2010 9:09:59 AM
36 Saturday, 15 May, 2010 09:09:59 AM
37 Saturday, 15 May, 2010 9:09:59
38 Saturday, 15 May, 2010 09:09:59
39 15 May, 2010 9:09:59 AM
40 15 May, 2010 09:09:59 AM
41 15 May, 2010 9:09:59
42 15 May, 2010 09:09:59
43 5/15/2010 9:09 AM
44 5/15/2010 09:09 AM
45 5/15/2010 9:09
46 5/15/2010 09:09
47 5/15/10 9:09 AM
48 5/15/10 09:09 AM
49 5/15/10 9:09
50 5/15/10 09:09
51 05/15/10 9:09 AM
52 05/15/10 09:09 AM
53 05/15/10 9:09
54 05/15/10 09:09
55 05/15/2010 9:09 AM
56 05/15/2010 09:09 AM
57 05/15/2010 9:09
58 05/15/2010 09:09
59 10/05/15 9:09 AM
60 10/05/15 09:09 AM
61 10/05/15 9:09
62 10/05/15 09:09
63 2010-05-15 9:09 AM
64 2010-05-15 09:09 AM
65 2010-05-15 9:09
66 2010-05-15 09:09
67 15-May-10 9:09 AM
68 15-May-10 09:09 AM
69 15-May-10 9:09
70 15-May-10 09:09
71 5/15/2010 9:09:59 AM
72 5/15/2010 09:09:59 AM
73 5/15/2010 9:09:59
74 5/15/2010 09:09:59
75 5/15/10 9:09:59 AM
76 5/15/10 09:09:59 AM
77 5/15/10 9:09:59
78 5/15/10 09:09:59
79 05/15/10 9:09:59 AM
80 05/15/10 09:09:59 AM
81 05/15/10 9:09:59
82 05/15/10 09:09:59
83 05/15/2010 9:09:59 AM
84 05/15/2010 09:09:59 AM
85 05/15/2010 9:09:59
86 05/15/2010 09:09:59
87 10/05/15 9:09:59 AM
88 10/05/15 09:09:59 AM
89 10/05/15 9:09:59
90 10/05/15 09:09:59
91 2010-05-15 9:09:59 AM
92 2010-05-15 09:09:59 AM
93 2010-05-15 9:09:59
94 2010-05-15 09:09:59
95 15-May-10 9:09:59 AM
96 15-May-10 09:09:59 AM
97 15-May-10 9:09:59
98 15-May-10 09:09:59
99 May 15
100 May 15
101 2010-05-15T09:09:59.0464946-07:00
102 2010-05-15T09:09:59.0464946-07:00
103 Sat, 15 May 2010 09:09:59 GMT
104 Sat, 15 May 2010 09:09:59 GMT
105 2010-05-15T09:09:59
106 9:09 AM
107 09:09 AM
108 9:09
109 09:09
110 9:09:59 AM
111 09:09:59 AM
112 9:09:59
113 09:09:59
114 2010-05-15 09:09:59Z
115 Saturday, May 15, 2010 4:09:59 PM
116 Saturday, May 15, 2010 04:09:59 PM
117 Saturday, May 15, 2010 16:09:59
118 Saturday, May 15, 2010 16:09:59
119 May 15, 2010 4:09:59 PM
120 May 15, 2010 04:09:59 PM
121 May 15, 2010 16:09:59
122 May 15, 2010 16:09:59
123 Saturday, 15 May, 2010 4:09:59 PM
124 Saturday, 15 May, 2010 04:09:59 PM
125 Saturday, 15 May, 2010 16:09:59
126 Saturday, 15 May, 2010 16:09:59
127 15 May, 2010 4:09:59 PM
128 15 May, 2010 04:09:59 PM
129 15 May, 2010 16:09:59
130 15 May, 2010 16:09:59
131 May, 2010
132 May, 2010
Comments
Anonymous
May 15, 2010
Nice one liner. Another way to skin the cat: (Get-Date).GetDateTimeFormats() | % {$c=0} { New-Object PSObject -Property @{index=++$c; output=$_} } | ft index, output -aAnonymous
May 15, 2010
I'm also a big fan of the new -property parameter to new-object.