PowerShell Date Format

In this tutorial we will go through PowerShell Date Format. Date and time are used many times throughout our scripts. You can get the current date of a system by using Get-Date cmdlet. The default format is defined by the system locale that you are running the command on. We will go though some formatting of the date value and see the differences using the available parameters for this cmdlet. I will not go though all parameters of Get-Date cmdlet as this tutorial is only focused on formatting the display of the date.

Formatting Parameters:

DisplayHint

Format

UFormat

Below you can see the default format for my system when I use Get-Date.

If we will use Format-List , we will see all the information that is included. Get-Date cmdlet returns a DateTime object. Below is output of the same command, showing all the information.

Get-Date | Format-List

Output:

DisplayHint

First we will use -DisplayHint parameter. By using this parameter, we define which values of the information we want to be displayed. The object returned by the cmdlet is still DateTime object. This parameter can accept three values.

Date – Shows only the date

Time – Shows only the time

DateandTime – Shows both date and time. This is the default value.

Format

By using -Format parameter you are allow to display date and time in the format that you specify. -Format parameter accepts all the values that are allowed in Microsoft .Net Framework that are included in DateTimeFormatInfo Class. There are several options that you are able to use to format the date according to your needs. Note that the result provided when using -Format parameter is no longer DateTime object. The output of the command is String. Below is the information as provided by Microsoft for the available standard format specifiers and their description.

Standard Format Strings

Options list

Date: d : Short date pattern D : Long date pattern f : Full date/time pattern (short time) F : Full date/time pattern (long time) g : General date/time pattern (short time) G : General date/time pattern (long time) M or m : Month day pattern o : Round-trip date/time pattern R or r : RFC1123 pattern s : Sortable date/time pattern; conforms to ISO 8601 u : Universal sortable date/time pattern U : Universal sortable date/time pattern Y or y : Year month pattern

Time: t : Short time pattern T : Long time pattern



Let’s see the output of the below format examples:

Get-Date -Format D Get-Date -Format f Get-Date -Format F Get-Date -Format g Get-Date -Format G Get-Date -Format m Get-Date -Format y

Output of the above commands:

Custom Format Strings

The information below shows the custom formatting values that we can use with -Format parameter. The information below is provided by Microsoft.

Period/Era: g or gg (plus any number of additional “g” specifiers) : Represents the period or era (A.D. for example). This specifier is ignored if the date to be formatted does not have an associated period or era string.

Date: K : Represents different values of the DateTime.Kind property, that is, Local, Utc, or Unspecified. This specifier round-trips the kind value in text and preserves the time zone.

Year: y : Represents the year as at most a two-digit number. If the year has more than two digits, only the two low-order digits appear in the result. If the year has fewer than two digits, the number is formatted without a leading zero. yy : Represents the year as a two-digit number. If the year has more than two digits, only the two low-order digits appear in the result. If the year has fewer than two digits, the number is padded with leading zeroes to achieve two digits. yyy : Represents the year as a three-digit number. If the year has more than three digits, only the three low-order digits appear in the result. If the year has fewer than three digits, the number is padded with leading zeroes to achieve three digits. Note that for the Thai Buddhist calendar, which can have five-digit years, this format specifier displays all five digits. yyyy : Represents the year as a four-digit number. If the year has more than four digits, only the four low-order digits appear in the result. If the year has fewer than four digits, the number is padded with leading zeroes to achieve four digits. Note that for the Thai Buddhist calendar, which can have five-digit years, this format specifier renders all five digits. yyyyy (plus any number of additional “y” specifiers) : Represents the year as a five-digit number. If the year has more than five digits, only the five low-order digits appear in the result. If the year has fewer than five digits, the number is padded with leading zeroes to achieve five digits. If there are additional “y” specifiers, the number is padded with as many leading zeroes as necessary to achieve the number of “y” specifiers.

Day: d : Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero. dd : Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero. ddd : Represents the abbreviated name of the day of the week as defined in the current System.Globalization.DateTimeFormatInfo.AbbreviatedDayNames property. dddd (plus any number of additional “d” specifiers) : Represents the full name of the day of the week as defined in the current System.Globalization.DateTimeFormatInfo.DayNames property.

Time Zone: z : Represents the signed time zone offset of your system from Greenwich Mean Time (GMT) measured in hours. The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of GMT and a minus sign (-) indicates hours behind GMT. The offset ranges from â€“12 through +13. A single-digit offset is formatted without a leading zero. The offset is affected by daylight savings time. zz : Represents the signed time zone offset of your system from Greenwich Mean Time (GMT) measured in hours. The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of GMT and a minus sign (-) indicates hours behind GMT. The offset ranges from â€“12 through +13. A single-digit offset is formatted with a leading zero. The offset is affected by daylight savings time. zzz , zzz (plus any number of additional “z” specifiers) : Represents the signed time zone offset of your system from Greenwich Mean Time (GMT) measured in hours and minutes. The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of GMT and a minus sign (-) indicates hours behind GMT. The offset ranges from â€“12 through +13. A single-digit offset is formatted with a leading zero. The offset is affected by daylight savings time.

Time: t : Represents the first character of the A.M./P.M. designator defined in the current System.Globalization.DateTimeFormatInfo.AMDesignator or System.Globalization.DateTimeFormatInfo.PMDesignator property. tt , tt (plus any number of additional “t” specifiers) : Represents the A.M./P.M. designator as defined in the current System.Globalization.DateTimeFormatInfo.AMDesignator or System.Globalization.DateTimeFormatInfo.PMDesignator property.

Hours: h : Represents the hour as a number from 1 through 12, that is, the hour as represented by a 12-hour clock that counts the whole hours since midnight or noon. Consequently, a particular hour after midnight is indistinguishable from the same hour after noon. The hour is not rounded, and a single-digit hour is formatted without a leading zero. hh , hh (plus any number of additional “h” specifiers) : Represents the hour as a number from 01 through 12, that is, the hour as represented by a 12-hour clock that counts the whole hours since midnight or noon. Consequently, a particular hour after midnight is indistinguishable from the same hour after noon. The hour is not rounded, and a single-digit hour is formatted with a leading zero. H : Represents the hour as a number from 0 through 23, that is, the hour as represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted without a leading zero. HH , HH (plus any number of additional “H” specifiers) : Represents the hour as a number from 00 through 23, that is, the hour as represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted with a leading zero.

Minutes: m : Represents the minute as a number from 0 through 59. The minute represents whole minutes passed since the last hour. A single-digit minute is formatted without a leading zero. mm , mm (plus any number of additional “m” specifiers) : Represents the minute as a number from 00 through 59. The minute represents whole minutes passed since the last hour. A single-digit minute is formatted with a leading zero. M : Represents the month as a number from 1 through 12. A single-digit month is formatted without a leading zero. MM : Represents the month as a number from 01 through 12. A single-digit month is formatted with a leading zero. MMM : Represents the abbreviated name of the month as defined in the current System.Globalization.DateTimeFormatInfo.AbbreviatedMonthNames property. MMMM : Represents the full name of the month as defined in the current System.Globalization.DateTimeFormatInfo.MonthNames property.

Seconds: s : Represents the seconds as a number from 0 through 59. The second represents whole seconds passed since the last minute. A single-digit second is formatted without a leading zero. ss , ss (plus any number of additional “s” specifiers) : Represents the seconds as a number from 00 through 59. The second represents whole seconds passed since the last minute. A single-digit second is formatted with a leading zero. f : Represents the most significant digit of the seconds fraction. Note that if the “f” format specifier is used alone, without other format specifiers, it is interpreted as the “f” standard DateTime format specifier (full date/time pattern). ff : Represents the two most significant digits of the seconds fraction. fff : Represents the three most significant digits of the seconds fraction. ffff : Represents the four most significant digits of the seconds fraction. fffff : Represents the five most significant digits of the seconds fraction. ffffff : Represents the six most significant digits of the seconds fraction. fffffff : Represents the seven most significant digits of the seconds fraction. F : Represents the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero. FF : Represents the two most significant digits of the seconds fraction. However, trailing zeros, or two zero digits, are not displayed. FFF : Represents the three most significant digits of the seconds fraction. However, trailing zeros, or three zero digits, are not displayed. FFFF : Represents the four most significant digits of the seconds fraction. However, trailing zeros, or four zero digits, are not displayed. FFFFF : Represents the five most significant digits of the seconds fraction. However, trailing zeros, or five zero digits, are not displayed. FFFFFF : Represents the six most significant digits of the seconds fraction. However, trailing zeros, or six zero digits, are not displayed. FFFFFFF : Represents the seven most significant digits of the seconds fraction. However, trailing zeros, or seven zero digits, are not displayed.

Separators: : : The time separator defined in the current System.Globalization.DateTimeFormatInfo.TimeSeparator property that is used to differentiate hours, minutes, and seconds. / : The date separator defined in the current System.Globalization.DateTimeFormatInfo.DateSeparator property that is used to differentiate years, months, and days. " : Quoted string (quotation mark). Displays the literal value of any string between two quotation marks (“). Precede each quotation mark with an escape character (\). ' : Quoted string (apostrophe). Displays the literal value of any string between two apostrophe (‘) characters. %c : Represents the result associated with a custom format specifier “c”, when the custom DateTime format string consists solely of that custom format specifier. That is, to use the “d”, “f”, “F”, “h”, “m”, “s”, “t”, “y”, “z”, “H”, or “M” custom format specifier by itself, specify “%d”, “%f”, “%F”, “%h”, “%m”, “%s”, “%t”, “%y”, “%z”, “%H”, or “%M”. \c : The escape character. Displays the character “c” as a literal when that character is preceded by the escape character (\). To insert the backslash character itself in the result string, use two escape characters (“\\”).



Let’s see few examples using custom specifiers. We will use the below:

Get-Date -Format dd-MMM-yyyy Get-Date -Format dd-MMMM-yyyy Get-Date -Format MM-yy Get-Date -Format MM-yyyy Get-Date -Format MMMM-yy Get-Date -Format MMMM-yyyy Get-Date -Format 'dd-MMMM-yyyy hh:mm:ss tt' Get-Date -Format 'dd-MMMM-yyyy HH:mm:ss'

Output of the above commands:

UFormat

This parameters allows you to specify the format that you would like to be displayed in Unix Format. The format of the command you should follow is Get-Date -UFormat %\<value\> . The output of the cmdlet while using -UFormat is also String. Below is the list of values that you can input for this parameters including the description and examples provided by Microsoft.

Options list:

(default) : (Friday, June 16, 2006 10:31:27 AM)

c : Date and time – abbreviated (Fri Jun 16 10:31:27 2006)

: Date and time – abbreviated (Fri Jun 16 10:31:27 2006) Date: D : Date in mm/dd/yy format (06/14/06) x : Date in standard format for locale (09/12/07 for English-US)

Year: C : Century (20 for 2006) Y : Year in 4-digit format (2006) y : Year in 2-digit format (06) G : Same as ‘Y’ g : Same as ‘y’

Month: b : Month name – abbreviated (Jan) B : Month name – full (January) h : Same as ‘b’ m : Month number (06)

Week: W : Week of the year (00-52) V : Week of the year (01-53) U : Same as ‘W’

Day: a : Day of the week – abbreviated name (Mon) A : Day of the week – full name (Monday) u : 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) w : Same as ‘u’

Time: p : AM or PM r : Time in 12-hour format (09:15:36 AM) R : Time in 24-hour format – no seconds (17:45) T : Time in 24 hour format (17:45:52) X : Same as ‘T’ Z : Time zone offset from Universal Time Coordinate (UTC) (-07)

Hour: H : Hour in 24-hour format (17) I : Hour in 12 hour format (05) k : Same as ‘H’ l : Same as ‘I’ (Upper-case I = Lower-case L)

Minutes & Seconds: M : Minutes (35) S : Seconds (05) s : Seconds elapsed since January 1, 1970 00:00:00 (1150451174.95705)

Special Characters: n : newline character (

) t : Tab character (\t)



Below I will use the same examples that I have used with -Format but change it with -UFormat parameter:

Get-Date -UFormat %d-%m-%Y Get-Date -UFormat %d-%b-%Y Get-Date -UFormat %d-%B-%Y Get-Date -UFormat %m-%y Get-Date -UFormat %m-%Y Get-Date -UFormat %B-%y Get-Date -UFormat %B-%Y Get-Date -UFormat '%d-%B-%Y %r' Get-Date -UFormat '%d-%B-%Y %I:%M:%S %p' Get-Date -UFormat '%d-%B-%Y %T' Get-Date -UFormat '%d-%B-%Y %H:%M:%S'

Output of the above commands:

Summary:

There are a lot of options that you can use to format the display of Date and Time. Either you can use one of the standard formatting or use the custom formatting. The best way, is to play with those options and find out the exact display format that you would like to use in your scripts.

I hope the tutorial about Date Time formatting is helpful.

Please let me know your comments and thoughts. You feedback is appreciated.

Related Links: