This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
[edit -
as u/anynonus pointed out, i failed to test this code. it glitches when the 1st day of the month is either sunday or monday. time for some more testing ... [grin]
edit 2 -
fixed it with a switch to handle sunday/monday as 1st of month.
seems to work correctly now ...
1..12 | ForEach-Object {Get-FirstMondayOfMonth -MonthNumber $_}
]
howdy y'all,
needed a way to find the 1st "some day of month" and decided that it was easier to grab the 1st monday and use that to find the 1st whatever. thus the following code ...
function Get-FirstMondayOfMonth
{
<#
comment based help goes here
#>
[CmdletBinding ()]
Param (
[Parameter(
Position = 0
)]
[ValidateRange (1,12)]
[int]
$MonthNumber = (Get-Date).Month
)
if ($MonthNumber)
{
$Day_1 = Get-Date -Month $MonthNumber -Day 1
}
else
{
$Day_1 = Get-Date -Day 1
}
switch ($Day_1.DayOfWeek.value__)
{
0 {
$Offset = 1
break
}
1 {
$Offset = 0
break
}
default {
# the " 1" is to handle the "off-by-one" array index difference
$Offset = 7 - $Day_1.Date.DayOfWeek.value__ 1
}
}
$Day_1.Date.AddDays($Offset)
}
other than the lack of CBH [grin], is there anything that i otta change?
take care,
lee
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/PowerShell/...