Kevin
Kevin

Reputation: 161

Date time parsing to another format

I need to get the end date of the month we are in. I tried with this code:

$CURRENTDATE=GET-DATE -Format "dd/MM/yyyy"
$FIRSTDAYOFMONTH=GET-DATE $CURRENTDATE -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)

echo $LASTDAYOFMONTH

This gives me the date of the end of the month. But i need it in another format (dd/MM/yyyy). But can't seem to get my head around it. A push in the right direction would be great.

Upvotes: 0

Views: 54

Answers (2)

henrycarteruk
henrycarteruk

Reputation: 13217

Or if you want to use your existing code:

$CurrentDate = Get-Date -Format "dd/MM/yyyy"
$FirstDayOfMonth = Get-Date $CurrentDate -Day 1
$LastDayOfMonth = Get-Date $FirstDayOfMonth.AddMonths(1).AddSeconds(-1) -Format "dd/MM/yyyy"

Write-Output $LastDayOfMonth

Upvotes: 1

TechSpud
TechSpud

Reputation: 3518

Adapting Mike F. Robbins code

$currentDate = Get-Date
$lastDay = [DateTime]::DaysInMonth($currentDate.Year, $currentDate.Month)
$lastDayOfMonth = Get-Date ([DateTime]"$($currentDate.Month), $LastDay, $($currentDate.Year)") -Format 'dd/MM/yyyy'
Write-Host $lastDayOfMonth

Upvotes: 1

Related Questions