Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve
Created July 13, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XPlantefeve/8025acf4e2e5b7c827e457c74c29353c to your computer and use it in GitHub Desktop.
Save XPlantefeve/8025acf4e2e5b7c827e457c74c29353c to your computer and use it in GitHub Desktop.
Get date for last <day of week>
function Get-LastDOW {
<#
.SYNOPSIS
Returns the date of the last day of week you select.
.DESCRIPTION
Get-LastDOW will return the date of the last day of the week you select,
up to one week in the past (between one week ago and yesterday). Useful,
as an example, to search in logs for events only happening once a week.
.PARAMETER DayOfWeek
The day of the week you're searching the date for. Either the full name
of the day or its index in the week.
.EXAMPLE
Get-LastDOW -DayOfWeek Sunday
Gives the date of the last Sunday.
.INPUTS
[DayOfWeek] Enumeration.
.OUTPUTS
[DateTime]
#>
param
(
[DayOfWeek]
[Parameter(Mandatory=$true,HelpMessage='Please provide the day of week')]
$DayOfWeek
)
if ( [int]$DayOfWeek -ge [int]((Get-Date).DayOfWeek) ) { $adjust = 7 } else { $adjust = 0 }
return (Get-Date).AddDays( ( [int]$DayOfWeek ) - ( [int]((Get-Date).DayOfWeek) ) - $adjust )
}
Get-LastDOW -DayOfWeek Monday
Get-LastDOW -DayOfWeek Tuesday
Get-LastDOW -DayOfWeek Wednesday
Get-LastDOW -DayOfWeek Thursday
Get-LastDOW -DayOfWeek Friday
Get-LastDOW -DayOfWeek Saturday
Get-LastDOW -DayOfWeek Sunday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment