How to resolve the algorithm Last Friday of each month step by step in the PowerShell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Last Friday of each month step by step in the PowerShell programming language
Table of Contents
Problem Statement
Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).
Example of an expected output:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the PowerShell programming language
Source code in the powershell programming language
function last-dayofweek {
param(
[Int][ValidatePattern("[1-9][0-9][0-9][0-9]")]$year,
[String][validateset('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')]$dayofweek
)
$date = (Get-Date -Year $year -Month 1 -Day 1)
while($date.DayOfWeek -ne $dayofweek) {$date = $date.AddDays(1)}
while($date.year -eq $year) {
if($date.Month -ne $date.AddDays(7).Month) {$date.ToString("yyyy-dd-MM")}
$date = $date.AddDays(7)
}
}
last-dayofweek 2012 "Friday"
function Get-Date0fDayOfWeek
{
[CmdletBinding(DefaultParameterSetName="None")]
[OutputType([datetime])]
Param
(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateRange(1,12)]
[int]
$Month = (Get-Date).Month,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[ValidateRange(1,9999)]
[int]
$Year = (Get-Date).Year,
[Parameter(Mandatory=$true, ParameterSetName="Sunday")]
[switch]
$Sunday,
[Parameter(Mandatory=$true, ParameterSetName="Monday")]
[switch]
$Monday,
[Parameter(Mandatory=$true, ParameterSetName="Tuesday")]
[switch]
$Tuesday,
[Parameter(Mandatory=$true, ParameterSetName="Wednesday")]
[switch]
$Wednesday,
[Parameter(Mandatory=$true, ParameterSetName="Thursday")]
[switch]
$Thursday,
[Parameter(Mandatory=$true, ParameterSetName="Friday")]
[switch]
$Friday,
[Parameter(Mandatory=$true, ParameterSetName="Saturday")]
[switch]
$Saturday,
[switch]
$First,
[switch]
$Last,
[switch]
$AsString,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]
$Format = "dd-MMM-yyyy"
)
Process
{
[datetime[]]$dates = 1..[DateTime]::DaysInMonth($Year,$Month) | ForEach-Object {
Get-Date -Year $Year -Month $Month -Day $_ -Hour 0 -Minute 0 -Second 0 |
Where-Object -Property DayOfWeek -Match $PSCmdlet.ParameterSetName
}
if ($First -or $Last)
{
if ($AsString)
{
if ($First) {$dates[0].ToString($Format)}
if ($Last) {$dates[-1].ToString($Format)}
}
else
{
if ($First) {$dates[0]}
if ($Last) {$dates[-1]}
}
}
else
{
if ($AsString)
{
$dates | ForEach-Object {$_.ToString($Format)}
}
else
{
$dates
}
}
}
}
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday -AsString
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday -AsString -Format yyyy-MM-dd
You may also check:How to resolve the algorithm User input/Graphical step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the Julia programming language
You may also check:How to resolve the algorithm Pig the dice game/Player step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm History variables step by step in the Rust programming language