Question:
I am trying to format a date
in PowerShell as DayOfWeek Month Day(st|nd|rd|th) year
format.
1 2 |
(Get-Date).AddDays(+1).ToString('D') |
The output is “Tuesday, July 05, 2016”
but need it in this format: Tuesday July 5th 2016
1 2 3 4 5 |
if date(Get-Date) % 100 IN (11, 12, 13) THEN 'th' if (Get-Date) % 10 = 1 THEN 'st' if (Get-Date) % 10 = 2 THEN 'nd' if (Get-Date) % 10 = 3 THEN 'rd' |
Answer:
One possible solution could look like this
1 2 3 4 |
function Get-DateOrdinalSuffix([datetime]$Date) { switch -regex ($Date.Day.ToString()) { '1(1|2|3) |
Source:
Powershell get-date in 1st,2nd,2rd,4th Formats by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
{ 'th'; break }
'.?1
Source:
Powershell get-date in 1st,2nd,2rd,4th Formats by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
{ 'st'; break }
'.?2
Source:
Powershell get-date in 1st,2nd,2rd,4th Formats by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
{ 'nd'; break }
'.?3
Source:
Powershell get-date in 1st,2nd,2rd,4th Formats by stackoverflow.com 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