Hi Guys,
As I told you couple of weeks ago I am using a custom function to enhanced the Get-Date with information.
For my comfort I wished to add a dedicated method to add a week to the selected date.
Before having this idear when I wanted to add a week to the actual date I want. I was forced to use the AddDays() method using 7, 14, 21 and so on. What I found more comfy is to use directly 1, 2, 3 and so on matching the number of weeks you want to add.
To add my own method to the existing Get-Date cmdlet I will re-use my previous function Get-ExtendedDate. To add the method I used the Add-Member cmdlet with the following script:
[ps]#Script Methods to Add Weeks directly instead of days (7, 14, 21, 28 and so on)
Add-Member -MemberType ScriptMethod -Name AddWeeks -Value {
Param(
[Parameter(
Mandatory = $true,
Position = 0
)]
[Int]$Int
)
Get-ExtendedDate -Date $this.AddDays($Int*7)
} -InputObject $Output[/ps]
Calling back the Get-ExtendedDate allowed me to gather all the other properties I added for my previous needs to the actual one.
Here is the new function Get-ExtendedDate code:
[ps]Function Get-ExtendedDate {
Param(
[DateTime]$Date = $(Get-Date)
)
$Output = Get-Date -Date $Date
#Number of the current Week in the year
Add-Member -MemberType NoteProperty -Name WeekOfYear -Value (Get-Date -Date $Output -UFormat %V) -inputobject $Output
#Date of the first and last day of the current week
switch ($Output.DayOfWeek.value__) {
‘1’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output -Hour 00 -Minute 00 -Second 00 -Millisecond 00)-InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(6) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘2’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-1) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(5) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘3’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-2) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(4) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘4’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-3) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(3) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘5’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-4) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(2) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘6’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-5) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output.AddDays(1) -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
‘7’ {
Add-Member -MemberType NoteProperty -Name FirstDayOfWeek -Value (Get-Date $Output.AddDays(-6) -Hour 00 -Minute 00 -Second 00 -Millisecond 00) -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfWeek -Value (Get-Date $Output -Hour 23 -Minute 59 -Second 59 -Millisecond 59) -InputObject $Output
}
}
#Date of the first and last day of the current month
$FirstDayOfMonth = Get-Date -Date $Output -Day 1 -Hour 00 -Minute 00 -Second 00 -Millisecond 00
Add-Member -MemberType NoteProperty -Name FirstDayOfMonth -Value $FirstDayOfMonth -InputObject $Output
Add-Member -MemberType NoteProperty -Name LastDayOfMonth -Value $FirstDayOfMonth.AddMonths(1).AddMilliseconds(-1) -InputObject $Output
#Script Methods to Add Weeks directly instead of days (7, 14, 21, 28 and so on)
Add-Member -MemberType ScriptMethod -Name AddWeeks -Value {
Param(
[Parameter(
Mandatory = $true,
Position = 0
)]
[Int]$Int
)
Get-ExtendedDate -Date $this.AddDays($Int*7)
} -InputObject $Output
Write-Output -InputObject $Output
}[/ps]
The usage is quite simple. As you where using AddDays(X) method, you are using AddWeeks(X) method to add or remove the X number of weeks to the targeted date. Here is an example:
See you,