Have you ever wondered how many time your pay day has fallen on a certain day of the week. Especially Mondays as it makes for a long weekend when you are waiting to get paid.
Here is a quick little script that will tell you how many of your paydays in the last 10 years fell on a Monday. Assuming you get paid monthly and on the last day of the month.
Firstly we define the number of months
1 2 | $years = 10 $mths = ($years * 12) |
We now set the count to 0, this will be used to increment the number of paydays that fall on a Monday
1 | $count = 0 |
Now we need to work through each of the months, based on the number of years we entered,
1 2 3 | for($m=1 $m -le $mths $m++) |
During our pass through of each month, we need to query if our payday fell on a Monday. We do this by going to the first day of the month at the time 00:00, and subtract 1 second. This gives us the previous day (which would be the last day of the previous month).
1 2 | {$firstofmonth = Get-Date $date -day 1 -hour 0 -minute 0 -second 0 $lastofmonth = (($firstofmonth).AddMonths(-$m).AddSeconds(-1)) |
Then we use this date to get the day of the week. Now that we have the end of the month in a date format, we can check to see if that day was a Monday, and if so, show the day and date and increase the count by 1
1 2 3 4 5 | if ($lastofmonth.DayOfWeek -eq "Monday"){ Write-Host $lastofmonth.DayOfWeek $lastofmonth $count += 1 } } |
That’s it. You can modify the script as you see fit. As always feel free to comment.
Leave a Reply