New-CountdownDate.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 - The code is a PowerShell script that defines a CountdownDate
class and a New-CountdownDate
function. The CountdownDate
class has three properties: CountdownDay
, DaysUntilCountdown
, and CountdownTime
. The CountdownDay
property is a string that represents the date of the countdown. The DaysUntilCountdown
property is an integer that represents the number of days until the countdown. The CountdownTime
property is a TimeSpan
object that represents the time of the countdown.
The CountdownDate
class has a constructor that takes three parameters: CountdownDay
, DaysUntilCountdown
, and CountdownTime
. The constructor sets the CountdownDay
, DaysUntilCountdown
, and CountdownTime
properties to the values of the corresponding parameters.
The CountdownDate
class has a GetCountdown
method that returns an object that contains information about the countdown. The GetCountdown
method first checks whether the CountdownDay
property is set. If it is, the method tries to parse the CountdownDay
property as a date. If parsing fails, the method assumes that the CountdownDay
property is already a DateTime
object. If the CountdownDay
property is not set, the method calculates the countdown date by adding the DaysUntilCountdown
property to the current date.
The GetCountdown
method then calculates the number of days left until the countdown date and creates a new object that contains the current date, the countdown date, the countdown day, and the number of days left. The method returns the new object.
The New-CountdownDate
function is a cmdlet that takes two parameters: CountdownDay
and DaysUntilCountdown
. The function creates a new CountdownDate
object with the given parameters and calls the GetCountdown
method of the object. If an error occurs, the function writes the error message to the error stream.
To improve the code, we could add comments to explain the purpose of each method and property. We could also use more descriptive names for the properties and methods. Additionally, we could add error handling for cases where the CountdownDay
property is not set and the DaysUntilCountdown
property is negative. Finally, we could add unit tests to ensure that the code works correctly in all cases.
@BanterBoy: - Explain how to use the script and provide examples.
@GitHub Copilot - Certainly! Here are a few examples of how you could use the New-CountdownDate
function:
Example 1: Create a countdown for a specific date and time
New-CountdownDate -CountdownDay "2022-01-01" -CountdownTime "23:59:59"
This will create a countdown for January 1st, 2022 at 11:59:59 PM.
Example 2: Create a countdown for a specific number of days from now
New-CountdownDate -DaysUntilCountdown 30
This will create a countdown for 30 days from the current date and time.
Example 3: Create a countdown for a specific date and time and output the result to a file
New-CountdownDate -CountdownDay "2022-01-01" -CountdownTime "23:59:59" | Out-File "countdown.txt"
This will create a countdown for January 1st, 2022 at 11:59:59 PM and output the result to a file named “countdown.txt”.
Note that you can also pipe the output of the New-CountdownDate
function to other cmdlets or functions to further process the result.
Script
class CountdownDate {
[string]$CountdownDay
[int]$DaysUntilCountdown
[TimeSpan]$CountdownTime = [TimeSpan]::Zero
CountdownDate([string]$CountdownDay, [int]$DaysUntilCountdown, [TimeSpan]$CountdownTime = [TimeSpan]::Zero) {
$this.CountdownDay = $CountdownDay
$this.DaysUntilCountdown = $DaysUntilCountdown
$this.CountdownTime = $CountdownTime
}
[PSObject]GetCountdown() {
if ($this.CountdownDay) {
try {
$CountdownDate = [datetime]::ParseExact($this.CountdownDay, "dd/MM/yyyy", $null)
}
catch {
$CountdownDate = [datetime]$this.CountdownDay
}
}
elseif ($this.DaysUntilCountdown) {
$CountdownDate = (Get-Date).AddDays($this.DaysUntilCountdown)
}
else {
throw "Please specify either CountdownDay or DaysUntilCountdown"
}
$Today = Get-Date
$DaysLeft = New-TimeSpan -Start $Today -End $CountdownDate
$output = New-Object PSObject
$output | Add-Member -MemberType NoteProperty -Name "CurrentDate" -Value $Today.ToString("dd/MM/yyyy")
$output | Add-Member -MemberType NoteProperty -Name "CountdownDate" -Value $CountdownDate.ToString("dd/MM/yyyy")
$output | Add-Member -MemberType NoteProperty -Name "CountdownDay" -Value $CountdownDate.ToString("dddd dd MMMM")
$output | Add-Member -MemberType NoteProperty -Name "DaysLeft" -Value "$($DaysLeft.Days) Days"
return $output
}
}
function New-CountdownDate {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$CountdownDay,
[Parameter(Mandatory = $false)]
[int]$DaysUntilCountdown
)
try {
$countdown = [CountdownDate]::new($CountdownDay, $DaysUntilCountdown)
return $countdown.GetCountdown()
}
catch {
Write-Error $_.Exception.Message
}
}
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.