Get-PayDay.ps1


Description

@GitHub Copilot - Welcome @BanterBoy, I’m your Copilot and I’m here to help you get things done faster. I can identify issues, explain and even improve code.

You can ask generic questions, but what I’m really good at is helping you with your code. For example:

Generate unit tests for my code Explain the selected code Propose a fix for the bugs in my code If you want to learn more about my capabilities and limitations, check out the Copilot documentation.

I’m powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

@BanterBoy: - Explain what the script does and why it is useful.

@GitHub Copilot -


Script

Function Get-PayDay {
	<#
	.SYNOPSIS
		Get-PayDay - A function to calculate the next date that your payment will take place.

	.DESCRIPTION
		Get-PayDay - A function to calculate the next date that your payment will take place. The function tests to see if the expected payment date occurs on a weekend and displays the expected pay date. it is presumed that if the expected pay date falls on a Saturday or Sunday, then you would typically be paid on the Friday before your normal payday.

		Outputs inlcude
		[string]CurrentTime
		[string]Date
		[int]Day
		[string]DayOfWeek
		[string]LongDate
		[string]Month
		[string]Year

	.PARAMETER Day
		[int]Day - Enter value for the payment Day.

	.PARAMETER Month
		[string]Month - Enter value for the payment Day.

	.PARAMETER Year
		[string] Year - Enter value for the payment Year. Defaults to the current year.

	.PARAMETER CurrentTime
		[string] CurrentTime - Enter value for the current time. Expected format = HH:mm:ss. Defaults to the current time.

	.EXAMPLE
		Get-PayDay -Day 01 -Month January -Year 2020

		DayOfWeek   : Wednesday
		Day         : 1
		Month       : January
		Year        : 2020
		Date        : 01/01/2020
		LongDate    : 01 January 2020
		CurrentTime : 07:13:39

	.OUTPUTS
		System.String. Get-PayDay returns an object of type System.String.

	.NOTES
		Author:     Luke Leigh
		Website:    https://scripts.lukeleigh.com/
		LinkedIn:   https://www.linkedin.com/in/lukeleigh/
		GitHub:     https://github.com/BanterBoy/
		GitHubGist: https://gist.github.com/BanterBoy

	.INPUTS
		You can pipe objects to these perameters.

		- Day [string]
		- Nonth [string]
		- Year [string]
		- CurrentTume [string]

	.LINK
		https://scripts.lukeleigh.com
		Get-Date
		Write-Output
#>

	[CmdletBinding(DefaultParameterSetName = 'Default',
		ConfirmImpact = 'Medium',
		HelpUri = 'http://scripts.lukeleigh.com/',
		PositionalBinding = $true)]
	[OutputType([string], ParameterSetName = 'Default')]
	[Alias('gpd')]
	[OutputType([String])]
	Param
	(
		[Parameter(ParameterSetName = 'Default',
			Mandatory = $false,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			ValueFromRemainingArguments = $false,
			Position = 1,
			HelpMessage = '[int] Day - Enter value for the payment Day. Default value 30')]
		[ValidateRange(1, 31)]
		[int]
		$Day = 30,
		[Parameter(ParameterSetName = 'Default',
			Mandatory = $false,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			ValueFromRemainingArguments = $false,
			Position = 2,
			HelpMessage = '[string] Month - Enter value for the payment Day. Press TAB to cycle through the months or enter a partial and tab complete. Defaults to the current month.')]
		[ValidateSet('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', IgnoreCase = $true)]
		[string]
		$Month = (Get-Date).Month,
		[Parameter(ParameterSetName = 'Default',
			Mandatory = $false,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			ValueFromRemainingArguments = $false,
			Position = 3,
			HelpMessage = '[string] Year - Enter value for the payment Year. Defaults to the current year.')]
		[ValidatePattern('^[1-9]\d{3,}$')]
		[ValidateRange(1000, 2999)]
		[string]
		$Year = (Get-Date).Year,
		[Parameter(ParameterSetName = 'Default',
			Mandatory = $false,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			ValueFromRemainingArguments = $false,
			Position = 4,
			HelpMessage = '[string] CurrentTime - Enter value for the current time. Expected format = HH:mm:ss. Defaults to the current time.')]
		[ValidatePattern('^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$')]
		[string]
		$CurrentTime = (Get-Date).ToString('HH:mm:ss')
	)

	Begin {
	}

	Process {
		Try {
			$PayDay = [datetime] ([string]$Month + "/" + [string]$Day + "/" + [string]$Year)
			If ($PayDay.DayOfWeek -eq "Sunday") {
				$properties = [ordered]@{
					"DayOfWeek"   = $PayDay.AddDays( - 2).DayOfWeek
					"Day"         = $PayDay.AddDays( - 2).Day
					"Month"       = $PayDay.ToString('MMMM')
					"Year"        = $PayDay.Year
					"Date"        = $PayDay.AddDays( - 2).ToShortDateString()
					"LongDate"    = $PayDay.AddDays( - 2).ToLongDateString()
					"CurrentTime" = $CurrentTime
				}
			}
			ElseIf ($PayDay.dayofweek -eq "Saturday") {
				$properties = [ordered]@{
					"DayOfWeek"   = $PayDay.AddDays(-1).DayOfWeek
					"Day"         = $PayDay.AddDays(-1).Day
					"Month"       = $PayDay.ToString('MMMM')
					"Year"        = $PayDay.Year
					"Date"        = $PayDay.AddDays(-1).ToShortDateString()
					"LongDate"    = $PayDay.AddDays(-1).ToLongDateString()
					"CurrentTime" = $CurrentTime
				}
			}
			Else {
				$properties = [ordered]@{
					"DayOfWeek"   = $PayDay.DayOfWeek
					"Day"         = $PayDay.Day
					"Month"       = $PayDay.ToString('MMMM')
					"Year"        = $PayDay.Year
					"Date"        = $PayDay.ToShortDateString()
					"LongDate"    = $PayDay.ToLongDateString()
					"CurrentTime" = $CurrentTime
				}
			}
		}
		Catch {
			Write-Error "Invalid date"
		}
		Finally {
			$obj = New-Object -TypeName PSObject -Property $properties
			Write-Output $obj
		}
	}

	End {
	}
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top