Powershell get-date in 1st,2nd,2rd,4th Formats

Question:

I am trying to format a date in PowerShell as DayOfWeek Month Day(st|nd|rd|th) year format.

The output is “Tuesday, July 05, 2016”
but need it in this format: Tuesday July 5th 2016

Answer:

One possible solution could look like this

Source:

Powershell get-date in 1st,2nd,2rd,4th Formats by licensed under CC BY-SA | With most appropriate answer!

{ 'th'; break }
'.?1

Source:

Powershell get-date in 1st,2nd,2rd,4th Formats by licensed under CC BY-SA | With most appropriate answer!

{ 'st'; break }
'.?2

Source:

Powershell get-date in 1st,2nd,2rd,4th Formats by licensed under CC BY-SA | With most appropriate answer!

{ 'nd'; break }
'.?3

Source:

Powershell get-date in 1st,2nd,2rd,4th Formats by licensed under CC BY-SA | With most appropriate answer!

{ 'rd'; break }
default { 'th'; break }
}
}

$d = (Get-Date).AddDays(1)
$suffix = Get-DateOrdinalSuffix $d
$dateFormatted = "{0} {1:MMMM} {2}{3} {4}" -f $d.DayOfWeek, $d, $d.Day, $suffix, $d.Year

Source:

Powershell get-date in 1st,2nd,2rd,4th Formats by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply