How can I check if today is the first Tuesday of the current quarter?

Viewed 70

I'm trying to find in Powershell if today's date is greater than the first Tuesday of the quarter or if it is before the first Tuesday of the quarter. The reason is my script should only make a change if today is past the first Tuesday of the quarter! I know how to do it for the first Tuesday of a month, but really struggling to get it to work with quarters, whats the best way to do this?

1 Answers
# Get today's date (without a time-of-day component).
$today = (Get-Date).Date

$afterFirstTueInQuarter =
  if ($today.Day -ge 7 -or $today.Month -notin 1, 4, 7, 10) { $true }
  else { # in the first 6 days of the start of a quarter
    # Find the first day of the month...
    $firstDayOfMonth = Get-Date -Year $today.Year -Month $today.Month -Day 1
    # ... and the month's first Tuesday.
    $firstTue = $firstDayOfMonth.AddDays(([DayOfWeek] 'Tuesday' - $firstDayOfMonth.DayOfWeek + 7) % 7)
    # Compare to today.
    $today -gt $firstTue
  }
Related