Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating custom date formats with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about working with Windows PowerShell is that it is highly configurable. In short, if there is something that I do not like, I can change it. Most of the time, that change is relatively simple to make. So when working with dates and times, there are lots of standardized ways of displaying the information. Note Also see Formatting Date Strings with PowerShell. But if one of these standardized ways doesn’t work out, I am not stuck. I can use the UFormat parameter to change the output to my needs. Keep in mind, that any formatting of the date/time output changes it from a DateTime object to a string. This means that I want to make sure I make calculations and computations with my date as an object before I output it as a string. The UFormat parameter uses Unix date formatting strings. I can, for example, display the date and the time by using the c string. Keep in mind that these are case sensitive, and I proceed them with the percentage sign. So, to display an abbreviated day of the week and month name, followed by the day of the month, the time, and the year, I use %c. This is shown here:
PS C:> get-date -UFormat %c
Thu Jan 22 15:39:36 2015 The cool thing is that for this command, I do not need to use quotation marks because Windows PowerShell expects me to supply a formatting string. I can also display a standard date output with respect to the local culture settings, but it does not appear to work properly. So I use the .NET formatting specifiers for this. This is shown here:
PS C:> Use-Culture de-DE {get-date -UFormat %x}
01.22.15
PS C:> Use-Culture de-DE {get-date -Format d}
22.01.2015 By using the UFormat codes, I can also control the way time outputs. For example, as shown here, I can display the time in 12-hour format by using %r.
PS C:> get-date -UFormat %r
04:33:09 PM I can also display a 24-hour format. A capital R outputs the hour and minute as shown here:
PS C:> get-date -UFormat %R
16:33 If I want to output the hour, minute, and seconds in 24-hour format, I use either capital T or capital X as shown here:
PS C:> get-date -UFormat %T
16:35:23
PS C:> get-date -UFormat %X
16:35:27 Most of the time when I use the UFormat strings, however, it is because I want to customize my output string. They are very flexible. I choose the parts of the date and time that I want to output. For example, suppose I need the year in four digits, month, day, hours in 24-hour format, minutes, and seconds, and I want them separated by an underscore:
PS C:> get-date -UFormat “%Y_%m_%d_%H_%M_%S”
2015_01_22_16_44_07 As you can see, there is a lot of flexibility. In fact, I can format the date and time any way that I can imagine. Here is a table that documents the Unix format strings. Note These letter codes are case sensitive.
Letter code |
Meaning |
Example |
c |
Date and time |
Fri Jun 16 10:31:27 2015 |
D |
Date in mm/dd/yy format |
06/14/06 |
x |
Date in standard format for locale |
09/12/15 for English-US |
C |
Century |
20 for 2015 |
Y, G |
Year in 4-digit format |
2015 |
y, g |
Year in 2-digit format |
15 |
b, h |
Month name – abbreviated |
Jan |
B |
Month name – full |
January |
m |
Month number |
06 |
W, U |
Week of the year – zero based |
00-52 |
V |
Week of the year – one based |
01-53 |
a |
Day of the week – abbreviated name |
Mon |
A |
Day of the week – full name |
Monday |
u, w |
Day of the week – number |
Monday = 1 |
d |
Day of the month – 2 digits |
05 |
e |
Day of the month – digit preceded by a space |
5 |
j |
Day of the year |
1-366 |
p |
AM or PM |
PM |
r |
Time in 12-hour format |
09:15:36 AM |
R |
Time in 24-hour format – no seconds |
17:45 |
T, X |
Time in 24 hour format |
17:45:52 |
Z |
Time zone offset from Universal Time Coordinate UTC |
07 |
H, k |
Hour in 24-hour format |
17 |
I, l |
Hour in 12 hour format |
05 |
M |
Minutes |
35 |
S |
Seconds |
05 |
s |
Seconds elapsed since January 1, 1970 |
00:00:00 1150451174.95705 |
n |
newline character |
n |
t |
Tab character |
t |
That is all there is to using UFormat strings to format dates. That is also the end of Date Time Week. Join me tomorrow when I will talk about the changing nature of documentation. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments