November 11th, 2013

PowerTip: Use PowerShell to Format Dates

Doctor Scripto
Scripter

Summary: Learn how to use Windows PowerShell to format dates.

Hey, Scripting Guy! Question How can I discover the formats that are available to use in Windows PowerShell when I format date and time information?

Hey, Scripting Guy! Answer One way is to keep this script handy and run it when you have a date formatting issue to output common formats:

“d ShortDatePattern                                                :  {0} ” -f (get-date -Format d )

“D LongDatePattern                                                 :  {0} ” -f (get-date -Format D )

“f Full date and time (long date and short time)                   :  {0} ” -f (get-date -Format f )

“F FullDateTimePattern (long date and long time)                   :  {0} ” -f (get-date -Format F )

“g General (short date and short time)                             :  {0} ” -f (get-date -Format g )

“G General (short date and long time)                              :  {0} ” -f (get-date -Format G )

“m MonthDayPattern                                                 :  {0} ” -f (get-date -Format m )

“M MonthDayPattern                                                 :  {0} ” -f (get-date -Format M )

“o Round-trip date/time pattern always uses the invariant culture  :  {0} ” -f (get-date -Format o )

“O Round-trip date/time pattern always uses the invariant culture  :  {0} ” -f (get-date -Format O )

“r RFC1123Pattern always uses the invariant culture                :  {0} ” -f (get-date -Format r )

“R RFC1123Pattern always uses the invariant culture                :  {0} ” -f (get-date -Format R )

“s SortableDateTimePattern always uses the invariant culture       :  {0} ” -f (get-date -Format s )

“t ShortTimePattern                                                :  {0} ” -f (get-date -Format t )

“T LongTimePattern                                                 :  {0} ” -f (get-date -Format T )

“u UniversalSortableDateTimePattern                                :  {0} ” -f (get-date -Format u )

“U Full date and time – universal time                             :  {0} ” -f (get-date -Format u )

“y YearMonthPattern                                                :  {0} ” -f (get-date -Format y )

“Y YearMonthPattern                                                :  {0} ” -f (get-date -Format Y )

 

“`nCustom Formats”

“d/M/y                                  :  {0} ” -f (get-date -Format d/M/y )

“%d/%M/yy                               :  {0} ” -f (get-date -Format d/M/yy )

“dd/MM/yyyy                             :  {0} ” -f (get-date -Format dd/MM/yyyyy )

“dd/MM/yyyy %g                          :  {0} ” -f (get-date -Format ‘dd/MM/yyyyy %g’)

“dd/MM/yyyy gg                          :  {0} ” -f (get-date -Format ‘dd/MM/yyyyy gg’)

“dddd dd/MM/yyyy gg                     :  {0} ” -f (get-date -Format ‘dddd dd/MM/yyyyy gg’)

“dddd dd/MM/yyyy %h:m:s tt gg           :  {0} ” -f (get-date -Format ‘dddd dd/MM/yyyyy %h:m:s tt gg’)

“dddd dd/MM/yyyy hh:mm:s tt gg          :  {0} ” -f (get-date -Format ‘dddd dd/MM/ yyyyy hh:mm:s tt gg’)

“dddd dd/MM/yyyy HH:mm:s gg             :  {0} ” -f (get-date -Format ‘dddd dd/MM/yyyyy HH:mm:s gg’)

“dddd dd/MM/yyyy HH:mm:s.ffff gg        :  {0} ” -f (get-date -Format ‘dddd dd/MM/yyyyy HH:mm:s.ffff gg’)

“dddd dd MMM yyyy HH:mm:s.ffff gg       :  {0} ” -f (get-date -Format ‘dddd dd MMM yyyyy HH:mm:s.ffff gg’)

“dddd dd MMMM yyyy HH:mm:s.ffff gg      :  {0} ” -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff gg’)

“dddd dd MMMM yyyy HH:mm:s.ffff zz gg   :  {0} ” -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff zz gg’)

“dddd dd MMMM yyyy HH:mm:s.ffff zzz gg  :  {0} ” -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff zzz gg’) 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.