Reputation: 741
I have this script which I found on the internet to calculate the third Tuesday of every month - but I need to modify it to simply give me the 7th day of the month, and the same but plus any number of days).
$FindNthDay=3
$WeekDay='Tuesday'
[datetime]$Today=[datetime]::NOW
$todayM=$Today.Month.ToString()
$todayY=$Today.Year.ToString()
[datetime]$StrtMonth=$todayM+'/7/'+$todayY
while ($StrtMonth.DayofWeek -ine $WeekDay ) { $StrtMonth=$StrtMonth.AddDays(1) }
$StrtMonth.AddDays(7*($FindNthDay-1))
$NextUpdate = $StrtMonth.AddDays(7*($FindNthDay-1))
I'm not understanding the logic here, I guess I don't need $Weekday. any ideas? I'd like to keep to the similar structure and variable names where possible in order to keep it consistent with the other scripts.
Thanks
Upvotes: 1
Views: 9983
Reputation: 1
This isn't the answer to the question, but my search led me here so hopefully someone finds this useful.
This will tell you which day today falls on (1st Friday of the month, 2nd Monday of the month, etc.). It only returns an integer.
Function Get-nthDayOfMonth {
$WorkingDay = (Get-Date).day
[int]$WeekCount = 1
$WeekFound = $false
While($WeekFound -eq $false){
$WorkingDay = $WorkingDay - 7
if ($WorkingDay -le 0){
$WeekFound = $true
}
else{
$WeekCount++
}
}
return $WeekCount
}
Upvotes: 0
Reputation: 3600
Today
get-date
Wednesday, April 25, 2018 5:23:34 PM
7th day of current month (using current time)
get-date -day 7
Saturday, April 07, 2018 5:24:15 PM
Just the date (midnight)
(get-date -day 7).date
Saturday, April 07, 2018 12:00:00 AM
Add days with ".adddays(3)"
(get-date -day 7).date.adddays(3)
Tuesday, April 10, 2018 12:00:00 AM
Full control of the date
(Get-Date -Year 2018 -Month 4 -Day 7).Date
Saturday, April 07, 2018 12:00:00 AM
The numbers could be variables
$yr=2018
$mn=4
$dy=7
(Get-Date -Year $yr -Month $mn -Day $dy).Date
Saturday, April 07, 2018 12:00:00 AM
Upvotes: 0
Reputation: 29866
Get-Date
accepts a -Day
argument, and it's value will be used as the day in the date:
PS> Get-Date -Day 7
Wednesday, September 07, 2016 17:47:36
Or if you want midnight:
PS> Get-Date '00:00:00' -Day 7
Wednesday, September 07, 2016 00:00:00
(Even though you can't see it in the output above, that also sets the fractional seconds to 0 as well.)
There's some sort of odd behavior if you go out past the end of the month without getting higher than 31 days. For example, September only has 30 days, and this is the result if you pass in 31:
PS> Get-Date -Month 9 -Day 31
Saturday, October 01, 2016 17:53:31
(-Month 9
above is just so that the result isn't sensitive to the current time for future copy/pasters.)
So you may want to validate that the output month matches the current one:
PS> (Get-Date -Month 9 -Day 31).Month -eq (Get-Date -Month 9).Month
False
If you pass in 32 or higher, it results in an error:
PS> Get-Date -Day 32
Get-Date : Cannot validate argument on parameter 'Day'. The 32 argument is greater than the maximum allowed range of 31. Supply an argument that is less than or equal to 31 and then try the command again.
Upvotes: 1
Reputation: 13537
This actually isn't too bad.
First, let's store a pointer to todays date in a variable, let's call it $date.
$date = Get-Date
Next, we'll need to figure out what numerical day of the month is (like the numerical portion of it). We can do that using $date.Day
.
$date.day
19
Now, let's use the .AddDays()
method to subtract today's date from the date, to give us a pointer to the first of the month.
$date.AddDays(-($date.Day-1))
>Thursday, September 1, 2016 11:46:36 AM
Finally, we can chain another .AddDays()
to the end of this long string, to add the number of days to it that you'd like to. In your case, you'd like to find the seventh day of the month. We'll do that by adding six more days.
$date.AddDays(-($date.Day-1)).AddDays(6)
>Wednesday, September 7, 2016 11:48:15 AM
Upvotes: 2