PowerTip: Use PowerShell to Format Dates

PowerTip: Use PowerShell to Format Dates

  • Comments 7
  • Likes

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') 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Why does this happen:

    Custom Formats

    d/M/y                                  :  12-11-13

    note the dashed rater than slashes.

    b.t.w:

    Get-Culture

    LCID             Name             DisplayName                                                                                                                            

    ----             ----             -----------                                                                                                                            

    1033             en-US            English (United States)                                    

  • My guess is that its due to cultural settings.

    I get this:

    £> get-date -Format d/M/y

    12/11/13

    what does Get-UICulture show on your machine

  • This:

    PS C:\WINDOWS\system32> Get-UICulture

    LCID             Name             DisplayName

    ----             ----             -----------

    1033             en-US            English (United States)

    PS C:\WINDOWS\system32> get-date -Format d/M/y

    12-11-13

    I get the slashes 'translated' to dashes. I agree it must be culture related, but it still seems strange.

  • In follow-up, looks like it's hiding in 'System.Globalization.DateTimeFormatInfo'. This seems to 'force' the dashes.

    PS C:\WINDOWS\system32> $bla = Get-UICulture

    PS C:\WINDOWS\system32> $bla.DateTimeFormat

    AMDesignator                     : AM

    Calendar                         : System.Globalization.GregorianCalendar

    DateSeparator                    : -

    FirstDayOfWeek                   : Sunday

    CalendarWeekRule                 : FirstDay

    FullDateTimePattern              : dddd, d MMMM, yyyy HH:mm:ss

    LongDatePattern                  : dddd, d MMMM, yyyy

    LongTimePattern                  : HH:mm:ss

    MonthDayPattern                  : MMMM d

    PMDesignator                     : PM

    RFC1123Pattern                   : ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

    ShortDatePattern                 : dd-MMM-yy

    ShortTimePattern                 : HH:mm

    SortableDateTimePattern          : yyyy'-'MM'-'dd'T'HH':'mm':'ss

    TimeSeparator                    : :

    UniversalSortableDateTimePattern : yyyy'-'MM'-'dd HH':'mm':'ss'Z'

    YearMonthPattern                 : MMMM yyyy

    AbbreviatedDayNames              : {Sun, Mon, Tue, Wed...}

    ShortestDayNames                 : {Su, Mo, Tu, We...}

    DayNames                         : {Sunday, Monday, Tuesday, Wednesday...}

    AbbreviatedMonthNames            : {Jan, Feb, Mar, Apr...}

    MonthNames                       : {January, February, March, April...}

    IsReadOnly                       : False

    NativeCalendarName               : Gregorian Calendar

    AbbreviatedMonthGenitiveNames    : {Jan, Feb, Mar, Apr...}

    MonthGenitiveNames               : {January, February, March, April...}

  • @Paul

    You have to look at the date formats to see if they have been altered.

    Get-UICulture |select -expand DateTimeFormat

  • Yep, its the dateSeperator

  • Get-UICulture | Select -ExpandProperty DateTimeFormat DateSeperator may be like '/' or '-'