1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# Find the date of a previous weekday. Originally conceived to export performance
# from a give day of the week no matter when it was called.
function Get-PreviousWeekdayDate
{
param
(
[DateTime]$date = $(Get-Date),
[System.DayOfWeek]$weekDay = $([System.DayOfWeek]::Sunday), # Full weekday names work here.
[switch]$fullWeek # The return date must be a full week ago.
)
Set-Variable -Name DAYS_IN_WEEK -Value 7 -Option Constant
$dayDelta = 0 # The difference in days between $date and the target day.
# If we are further in the week then we just go back x days.
if ([Int32]$date.DayOfWeek -ge [Int32]$weekDay)
{
$dayDelta = [Int32]$date.DayOfWeek – [Int32]$weekDay
}
# Otherwise we need wrap around the week
else
{
$dayDelta = $DAYS_IN_WEEK + [Int32]$date.DayOfWeek – [Int32]$weekDay
}
$date = $date.AddDays(-$dayDelta)
# Go back 7 days
if ($fullWeek)
{
$date = $date.AddDays(-$DAYS_IN_WEEK)
}
$date
}
|