Skip to content

Instantly share code, notes, and snippets.

@Smalls1652
Last active March 29, 2022 18:28
Show Gist options
  • Save Smalls1652/e4f61ad4a0829e50b0fbc4d2466de427 to your computer and use it in GitHub Desktop.
Save Smalls1652/e4f61ad4a0829e50b0fbc4d2466de427 to your computer and use it in GitHub Desktop.
Get-DayOfWeekOccurancesFromMonth
<#PSScriptInfo
.VERSION 2022.03.00
.GUID a91fcfe0-c9af-4d29-8a41-54de1e08f698
.AUTHOR Tim Small
.COMPANYNAME Smalls.Online
.COPYRIGHT 2022
.TAGS
.LICENSEURI
https://gist.githubusercontent.com/Smalls1652/e4f61ad4a0829e50b0fbc4d2466de427/raw/eb884e3adfa59976550f800ce7370c03b1d2a84b/LICENSE
.PROJECTURI
https://gist.github.com/Smalls1652/e4f61ad4a0829e50b0fbc4d2466de427
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
<#
.SYNOPSIS
Get each day of the week occurance for a month.
.DESCRIPTION
Get each day of the week occurance for a month that includes the week number, the day of the week provided, and the date time.
.PARAMETER InputDate
The datetime for the month you want to get occurances for.
.PARAMETER InputDayOfWeek
The day of the week to get the occurance for.
.EXAMPLE
PS C:\> Get-DayOfWeekOccurancesFromMonth.ps1
Get each Tuesday for the current month.
.EXAMPLE
PS C:\> Get-DayOfWeekOccurancesFromMonth.ps1 -InputDayOfWeek Monday
Get each Monday for the current month.
.EXAMPLE
PS C:\> Get-DayOfWeekOccurancesFromMonth.ps1 -InputDate "2022-07-01" -InputDayOfWeek Friday
Get each Friday for July 2022.
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[System.DateTime]$InputDate = [System.DateTime]::Now,
[Parameter(Position = 1)]
[System.DayOfWeek]$InputDayOfWeek = [System.DayOfWeek]::Tuesday
)
class DayOfWeekOccuranceItem {
[int]$WeekNumber
[System.DayOfWeek]$DayOfWeek
[System.DateTime]$DateTime
DayOfWeekOccuranceItem([int]$_weekNumber, [System.DayOfWeek]$_dayOfWeek, [System.DateTime]$_dateTime) {
$this.WeekNumber = $_weekNumber
$this.DayOfWeek = $_dayOfWeek
$this.DateTime = $_dateTime
}
}
# Take the input date and generate a DateTime object for the first of the month.
$firstDateOfMonth = [System.DateTime]::new($InputDate.Year, $InputDate.Month, 1)
# Evaluate the first day of the week for the month that was provided.
$firstDayOfWeekForMonth = $null
$firstDayOfWeekForMonthIsNotInFirstWeek = $false
switch ($firstDateOfMonth.DayOfWeek -eq $InputDayOfWeek) {
$true {
# If the first day of the month is the first provided day of the week of the month,
# then set it as the first provided day of the week of the month.
$firstDayOfWeekForMonth = $firstDateOfMonth
break
}
Default {
# If the first day of the month is not the first provided day of the week of the month,
# then calculate what the provided day of the week of that week is.
# Note: The day of the week could be in the previous month,
# but this is used to calculate what the first day of the week would be.
<#
This is calculated by subtracting the input day of the week from the day of the week for the first date of the month,
which is then used in the '.AddDays()' method of the first date of the month.
For example if the input day of the week is Tuesday and the first date of the month is a Friday,
It will calculate '2 (Tuesday) - 5 (Friday)', which will subtract -3 days from the first date of the month.
#>
$daysToAdd = ($InputDayOfWeek - $firstDateOfMonth.DayOfWeek)
$dateTimeOfDayOfWeek = $firstDateOfMonth.AddDays($daysToAdd)
if ($daysToAdd -lt 0) {
$firstDayOfWeekForMonthIsNotInFirstWeek = $true
}
# Check if the date/time of the day of the week is in the same month as the first date of the month.
# Then add it to the '$firstDayOfWeekForMonth' variable.
if ($dateTimeOfDayOfWeek.Month -ne $firstDateOfMonth.Month) {
# If it is not in the same month, add 7 days.
$firstDayOfWeekForMonth = $dateTimeOfDayOfWeek.AddDays(7)
}
else {
# If it is in the same month, do nothing.
$firstDayOfWeekForMonth = $dateTimeOfDayOfWeek
}
break
}
}
# Start outputting each day of the week for each week in the month.
# Set a variable for the current date being processed in the loop.
$currentDateInLoop = $firstDayOfWeekForMonth
# Run a for loop that increments by 1 while the current date in the loop's month is equal to the month.
for ($i = 1; $currentDateInLoop.Month -eq $firstDateOfMonth.Month; $i++) {
# Create a 'DayOfWeekOccuranceItem' object with:
# 1. The current loop counter.
# 2. The input day of the week.
# 3. The current date in the loop.
$occuranceItem = [DayOfWeekOccuranceItem]::new(
$i + [int]$firstDayOfWeekForMonthIsNotInFirstWeek,
$InputDayOfWeek,
$currentDateInLoop
)
# Write the interval item to the output.
Write-Output -InputObject $occuranceItem
# Increment the current date in the loop by 7 days.
$currentDateInLoop = $currentDateInLoop.AddDays(7)
}
MIT License
Copyright (c) 2022 Timothy Small
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment