Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/functions/public/Get-CurrentDateTime.ps1
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
25 changes: 25 additions & 0 deletions tests/PSModuleTest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading