Check for the first Tuesday that follows a Monday in a month

I am trying to figure out if there is a way to combine day/date checks to test the logical condition of whether today is the first Tuesday in the month that follows a Monday in the same month.

That is to say, if Tuesday is the first of the month, it would return False. For that month, it would not return true on any day except the 8th day (following the first Monday on the 7th of the month).

Benjamin,
Welcome to the forum. :wave:t4:

What have you tried so far? Please share your code along with an explanation what’s not working as expected.

Just in case … with

Get-Date | Format-List *

you can see all properties of the type [DateTime]. So with the properties Day and DayOfWeek you should be able to accomplish your task.

Suggestion - find the first Monday of the month and add 1 day to the date.

We had to do something similar to find a certain weekday after Patch Tuesday - find the 2nd Tuesday of the month and add X days.

try this …

[string]$month = (get-date).month
[string]$year = (get-date).year
$firstdayofmonth = [datetime] ([string]$month + "/1/" + [string]$year)
(0..30 | % {$firstdayofmonth.adddays($_) } | ? {$_.dayofweek -like "Tue*"})[0]

I would use

$date=Get-Date
(($date.DayOfWeek -eq "Tuesday") -and ($date.Day -le 8) -and ($date.Day -ge 2))

This will return true for the Tuesday following the first Monday, And respects the order of precedence for quicker results.

Nice. :+1:t4:

To make it a little more international you could use it like this:

$Today = Get-Date
if ($Today.DayOfWeek.value__ -eq 2 -and $Today.Day -ge 2 -and $Today.Day -le 8 ) {
    'Today is the first Tuesday of the month following a Monday of the same month'
}

You could shorten it slightly to this:

$Today = Get-Date
if ($Today.DayOfWeek.value__ -eq 2 -and $Today.Day -in 2..8 ) {
    'Today is the first Tuesday of the month following a Monday of the same month'
}

:roll_eyes:

:wink: :kissing_heart:

but that populates an array in the process so is slower, I did think of that though

and not to mention that the first statement in an -AND expression that is found $false the rest isn’t evaluated, so the order does make a difference

If it really is … do you really think it matters in such a case? :smirk:

Why would that make a difference?

it’s Tuesday rules out 6/7 days
its before 9th rules out 2/3ish of the month
then rules out 1st 1/9

as I said the first part that is found as false the rest isn’t evaluated, ok yes we are talking milliseconds or less, but that depends on how often the checks are made. its just good practice to think about running time.

all the solutions are good to be honest, I did like your international modification :smile:

Ah … you meant that it matters for speed, right? I misunderstood that.