From a246e7cd62188e6c5cfc52447bf64944888b820a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 20 Jan 2026 01:05:20 +0100 Subject: [PATCH 1/2] feat: Add Get-CurrentDateTime function --- src/functions/public/Get-CurrentDateTime.ps1 | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/functions/public/Get-CurrentDateTime.ps1 diff --git a/src/functions/public/Get-CurrentDateTime.ps1 b/src/functions/public/Get-CurrentDateTime.ps1 new file mode 100644 index 0000000..eb36af1 --- /dev/null +++ b/src/functions/public/Get-CurrentDateTime.ps1 @@ -0,0 +1,60 @@ +function Get-CurrentDateTime { + <# + .SYNOPSIS + Returns the current date and time in a specified format. + + .DESCRIPTION + Returns the current date and time formatted according to the specified format string. + Supports common format presets or custom format strings. + + .EXAMPLE + Get-CurrentDateTime + + Returns the current date and time in the default format (yyyy-MM-dd HH:mm:ss). + + .EXAMPLE + Get-CurrentDateTime -Format 'Short' + + Returns the current date in short date format. + + .EXAMPLE + Get-CurrentDateTime -Format 'Custom' -CustomFormat 'dddd, MMMM dd, yyyy' + + Returns the current date in a custom format like "Monday, January 20, 2026". + + .LINK + https://MariusStorhaug.github.io/MariusTestModule/Functions/Get-CurrentDateTime/ + #> + [OutputType([string])] + [CmdletBinding()] + param ( + # The format preset to use for the date and time output. + [Parameter()] + [ValidateSet('Default', 'Short', 'Long', 'ISO8601', 'Custom')] + [string] $Format = 'Default', + + # Custom format string when Format is set to 'Custom'. + [Parameter()] + [string] $CustomFormat = 'yyyy-MM-dd HH:mm:ss' + ) + + $currentDateTime = Get-Date + + switch ($Format) { + 'Default' { + $currentDateTime.ToString('yyyy-MM-dd HH:mm:ss') + } + 'Short' { + $currentDateTime.ToShortDateString() + } + 'Long' { + $currentDateTime.ToLongDateString() + } + 'ISO8601' { + $currentDateTime.ToString('o') + } + 'Custom' { + $currentDateTime.ToString($CustomFormat) + } + } +} From 6e1a7e16b65d8ab431929cb03b41e5efacb04852 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 20 Jan 2026 01:07:01 +0100 Subject: [PATCH 2/2] test: Add tests for Get-CurrentDateTime function --- tests/PSModuleTest.Tests.ps1 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 index 43f8710..01955e8 100644 --- a/tests/PSModuleTest.Tests.ps1 +++ b/tests/PSModuleTest.Tests.ps1 @@ -21,4 +21,29 @@ Describe 'Module' { It 'Function: Get-Greeting - Evening' { Get-Greeting -TimeOfDay 'Evening' | Should -Be 'Good Evening!' } + + It 'Function: Get-CurrentDateTime - Default format' { + $result = Get-CurrentDateTime + $result | Should -Match '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$' + } + + It 'Function: Get-CurrentDateTime - ISO8601 format' { + $result = Get-CurrentDateTime -Format 'ISO8601' + $result | Should -Match '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}' + } + + It 'Function: Get-CurrentDateTime - Custom format' { + $result = Get-CurrentDateTime -Format 'Custom' -CustomFormat 'yyyy' + $result | Should -Be (Get-Date).ToString('yyyy') + } + + It 'Function: Get-CurrentDateTime - Short format' { + $result = Get-CurrentDateTime -Format 'Short' + $result | Should -Not -BeNullOrEmpty + } + + It 'Function: Get-CurrentDateTime - Long format' { + $result = Get-CurrentDateTime -Format 'Long' + $result | Should -Not -BeNullOrEmpty + } }