#PowerShell DateTime Formatting Demystified

PowerShell provides a number of ways to format Date and Time thanks to its ability to tap into the .NET Framework.

By default, Get-Date returns the full date and time (e.g Sunday, January 8, 2017 11:00:00 AM) based on your system culture settings.

What if you want just the numeric representation of this result (e.g. 2017-01-08) or only the year (e.g. 2017) for use in some custom scripting?

This article shows you how you can fine-tune Get-Date using parameters to get the exact results that you want.  Some practical examples will also be shown.

The magic behind formatting the Get-Date result is the -format parameter.

Quick Formatting

Here is a handy chart that summarizes what you can specify with the -format parameter.

Specifier Format Sample Result
d ShortDatePattern 01/08/2017
D LongDatePattern Sunday, January 8, 2017
f Full date and time (long date and short time) Sunday, January 8, 2017 11:00 AM
F FullDateTimePattern (long date and long time with seconds) Sunday, January 8, 2017 11:00:00 AM
g General (short date and short time) 1/8/2017 11:00 AM
G General (short date and long time with seconds) 1/8/2017 11:00:24 AM
m, M MonthDayPattern January 8
o Round-trip date/time pattern 2017-01-08T11:00:00.4360943-05:00
r, R RFC1123Pattern Sun, 01 Jan 2017 11:00:00 GMT
s SortableDateTimePattern (based on ISO 8601) using local time 2017-01-08T11:00:00
t ShortTimePattern 11:00 AM
T LongTimePattern 11:00:00 AM
u UniversalSortableDateTimePattern using the format for universal time display 2017-01-08 11:00:00Z
U Full date and time (long date and long time) using universal time Sunday, January 8, 2017 11:00:00 AM
y, Y YearMonthPattern January 2017

Example 1:  ShortDatePattern

Get-Date -format d

Result:

1/8/2017

Example 2:  LongDatePattern

Get-Date -format D

Result:

Sunday, January 8, 2017

Advanced Formatting

The chart above shows you the built-in Date/Time formats. If you want something even more customized, you can use the following custom specifiers with the -format parameter.

Specifier Description
d. %d The day of the month. Single-digit days will not have a leading zero. Specify “%d” if the format pattern is not combined with other format patterns.
dd The day of the month. Single-digit days will have a leading zero.
ddd The abbreviated name of the day of the week.
dddd The full name of the day of the week, as defined in DayNames.
h, %h The hour in a 12-hour clock. Single-digit hours will not have a leading zero. Specify “%h” if the format pattern is not combined with other format patterns.
hh The hour in a 12-hour clock. Single-digit hours will have a leading zero.
H, %H The hour in a 24-hour clock. Single-digit hours will not have a leading zero. Specify “%H” if the format pattern is not combined with other format patterns.
HH The hour in a 24-hour clock. Single-digit hours will have a leading zero.
m, %m The minute. Single-digit minutes will not have a leading zero. Specify “%m” if the format pattern is not combined with other format patterns.
mm The minute. Single-digit minutes will have a leading zero.
M, %M The numeric month. Single-digit months will not have a leading zero. Specify “%M” if the format pattern is not combined with other format patterns.
MM The numeric month. Single-digit months will have a leading zero.
MMM The abbreviated name of the month, as defined in AbbreviatedMonthNames.
MMMM The full name of the month, as defined in MonthNames.
s, %s The second. Single-digit seconds will not have a leading zero. Specify “%s” if the format pattern is not combined with other format patterns.
ss The second. Single-digit seconds will have a leading zero.
t, %t The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. Specify “%t” if the format pattern is not combined with other format patterns.
tt The AM/PM designator defined in AMDesignator or PMDesignator, if any.
y, %y The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. Specify “%y” if the format pattern is not combined with other format patterns.
yy The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
yyy The year in three digits. If the year is less than 100, the year is displayed with a leading zero.
yyyy The year in four or five digits (depending on the calendar used), including the century. Will pad with leading zeroes to get four digits. Thai Buddhist and Korean calendars both have five digit years; users selecting the “yyyy” pattern will see all five digits without leading zeros for calendars that have five digits. Exception: the Japanese and Taiwan calendars always behave as if “yy” was selected.
%c Where c is a format pattern if used alone. That is, to use format pattern “d”, “f”, “F”, “h”, “m”, “s”, “t”, “y”, “z”, “H”, or “M” by itself, specify “%d”, “%f”, “%F”, “%h”, “%m”, “%s”, “%t”, “%y”, “%z”, “%H”, or “%M”. The “%” character can be omitted if the format pattern is combined with literal characters or other format patterns.
c Where c is any character. Displays the character literally. To display the backslash character, use “\”.

Example 3:  MMM d, yyyy @ HH:MM tt

Get-Date -format "MMM d, yyyy @ HH:MM tt"

Result:

Jan 8, 2017 @ 11:00 AM

Example 4:  Generate a filename with timestamp

Get-Date -format "Report_yyyyMMdd.txt"

Result:

Report_20170108.txt

Lots of cool possibilities with the PowerShell Get-Date command.

Give it a try!

Leave a Comment