Lee_Dailey

function Get-NthDayOfWeekInMonth

May 7th, 2022 (edited)
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-NthDayOfWeekInMonth
  2.     {
  3.     <#
  4.     .SYNOPSIS
  5.         Short description
  6.  
  7.     .DESCRIPTION
  8.         L-o-n-g
  9.         d-e-s-c-r-i-p-t-i-o-n
  10.  
  11.     .PARAMETER NthDayNumber
  12.         Which day instance you want. There are usually 4 of any given day, but sometimes there are 5.
  13.         The default is 2.
  14.  
  15.     .PARAMETER DayOfWeek
  16.         The day of the week to seek. This can be a day name OR a [System.DayOfWeek] object OR a number in 0..6 inclusive.
  17.         The default is Tuesday.
  18.  
  19.     .PARAMETER MonthNumber
  20.         The number of the month to work on.
  21.         The default is the current month.
  22.  
  23.     .PARAMETER YearNumber
  24.         The year number to work on.
  25.         The default is the current year.
  26.  
  27.     .EXAMPLE
  28.         Get-NthDayOfWeekInMonth
  29.  
  30.         Gets the 2nd Tuesday of the current month of the current year.
  31.  
  32.     .EXAMPLE
  33.         Get-NthDayOfWeekInMonth -NthDayNumber 3 -DayOfWeek Monday
  34.  
  35.         Gets the 3rd Monday of the current year & month.
  36.  
  37.     .INPUTS
  38.         Accepts [int32] for the *Number items, and a day name OR [DayOfWeek] enum value.
  39.     .OUTPUTS
  40.         Sends out a [DateTime] object.
  41.  
  42.     .NOTES
  43.         Source  = the ISE snippets
  44.         Version = 0.0.0.1
  45.     #>
  46.  
  47.     [CmdletBinding ()]
  48.     [Alias()]
  49.     [OutputType([DateTime])]
  50.     Param (
  51.         # NthDayNumber help description goes here
  52.         [Parameter (
  53.             Position = 0,
  54.             ValueFromPipeline,
  55.             ValueFromPipelineByPropertyName
  56.             )]
  57.             [ValidateRange (1,5)]
  58.             [int]
  59.             $NthDayNumber = 2,
  60.  
  61.         # DayOfWeek help description goes here
  62.         [Parameter (
  63.             Position = 1,
  64.             ValueFromPipelineByPropertyName
  65.             )]
  66.             [System.DayOfWeek]
  67.             $DayOfWeek = [System.DayOfWeek]::Tuesday,
  68.  
  69.         # MonthNumber help description goes here
  70.         [Parameter (
  71.             Position = 2,
  72.             ValueFromPipelineByPropertyName
  73.             )]
  74.             [ValidateRange (1,12)]
  75.             [int]
  76.             $MonthNumber = (Get-Date).Month,
  77.  
  78.         # YearNumber help description goes here
  79.         [Parameter (
  80.             Position = 3,
  81.             ValueFromPipelineByPropertyName
  82.             )]
  83.             [ValidateRange (1950, 2199)]
  84.             [int]
  85.             $YearNumber = (Get-Date).Year
  86.         )
  87.  
  88.     begin {}
  89.  
  90.     process
  91.         {
  92.         $FirstDayOfMonth = Get-Date -Year $YearNumber -Month $MonthNumber -Day 1
  93.  
  94.         # i have no idea what the logic behind the ofset number is
  95.         #    i just fiddled with it until it came out right [*grin*]
  96.         $Offset = (($DayOfWeek.value__ - $FirstDayOfMonth.DayOfWeek.value__ + 7) % 7) + (7 * ($NthDayNumber - 1))
  97.         $TargetWeekDay = $FirstDayOfMonth.Date.AddDays($Offset)
  98.  
  99.         # correct for "5th weekday overflow"
  100.         if ($TargetWeekDay.Month -ne $MonthNumber)
  101.             {
  102.             $TargetWeekDay = $TargetWeekDay.AddDays(-7)
  103.             }
  104.  
  105.         $TargetWeekDay
  106.         }
  107.    
  108.     end {}
  109.  
  110.     }
  111.  
Add Comment
Please, Sign In to add comment