Home > Software design >  Powershell how to check if friday is the last day of the month , returning True from a Function
Powershell how to check if friday is the last day of the month , returning True from a Function

Time:02-02

I have tried to make a function in Powershell that produces a True if the last day of the month is friday.

function Get-LastFridayOfMonth([DateTime] $d) {
    $lastDay = new-object DateTime($d.Year, $d.Month,[DateTime]::DaysInMonth($d.Year, $d.Month))
    $diff = ([int] [DayOfWeek]::Friday) - ([int] $lastDay.DayOfWeek)
    if ($diff -gT 0) {
        return $lastDay.AddDays(- (7-$diff))
    } else {
        return $lastDay.AddDays($diff)
    }
}
$testdate = Get-LastFridayOfMonth (Get-Date)

But how to check if that friday is the last day of the month ?

CodePudding user response:

I would change that function to just return the last days weekday so after calling it you can decide what weekday you get for the given date.

function Get-LastDayOfWeek {
    param (
        [datetime]$Date = (Get-Date)
    )
    # .AddDays(-$Date.Day 1) sets the given date to the 1st of that month
    $Date.AddDays(-$Date.Day 1).AddMonths(1).AddDays(-1).DayOfWeek
}

switch (Get-LastDayOfWeek) {
    'Friday' { "It's a Friday !" }
    default  { $_ }
}

Using the current date (February 2023) it would yield Tuesday

If you specify for instance March 2023

$d = Get-Date -Year 2023 -Month 3 -Day 1
switch (Get-LastDayOfWeek -Date $d) {
    'Friday' { "It's a Friday !" }
    default  { $_ }
}

The outcome will be It's a Friday !


If however you want a function to simply return $true or $false when the month end in a Friday or not, do this instead:

function Test-LastDayOfWeekIsFriday {
    param (
        [datetime]$Date = (Get-Date)
    )
    # .AddDays(-$Date.Day 1) sets the given date to the 1st of that month
    $Date.AddDays(-$Date.Day 1).AddMonths(1).AddDays(-1).DayOfWeek
}

Test-LastDayOfWeekIsFriday                                              # --> $false
Test-LastDayOfWeekIsFriday -Date (Get-Date -Year 2023 -Month 3 -Day 1)  # --> $true

Special thanks to mklement0 for the $Date.AddDays(-$Date.Day 1) part which always sets the date to the first day of that month

CodePudding user response:

@Theo's answer reframes your question and gives a nice all-in-one solution, but to address your specific question:

"how to check if [a date] is the last day of the month ?"

you can just add one day to the date and see if the month changes. If it does change then the original date must be the last one in the month…

$testdate = [datetime] "2023-01-31"
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# True

$testdate = [datetime] "2023-01-30"
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# False

$testdate = Get-LastFridayOfMonth (Get-Date)
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# depends on if the month ends on a friday
  • Related