Skip to content

Commit 94e3625

Browse files
🚀 [Feature]: Add Get-CurrentDateTime function (#14)
The module now includes a new `Get-CurrentDateTime` function that provides flexible date and time formatting options. You can retrieve the current date/time in multiple preset formats or specify your own custom format string, making it easier to generate timestamps for logging, file naming, or display purposes. - Fixes #15 ## New function: Get-CurrentDateTime A new public function `Get-CurrentDateTime` is available with the following format options: | Format | Description | Example Output | |--------|-------------|----------------| | `Default` | Standard timestamp format | `2026-01-20 14:30:45` | | `Short` | Locale-specific short date | `1/20/2026` | | `Long` | Locale-specific long date | `Monday, January 20, 2026` | | `ISO8601` | ISO 8601 format with timezone | `2026-01-20T14:30:45.1234567+01:00` | | `Custom` | User-defined format string | Depends on `-CustomFormat` parameter | ### Usage examples ```powershell # Get current date/time in default format Get-CurrentDateTime # Returns: 2026-01-20 14:30:45 # Get ISO 8601 formatted timestamp Get-CurrentDateTime -Format 'ISO8601' # Returns: 2026-01-20T14:30:45.1234567+01:00 # Use a custom format Get-CurrentDateTime -Format 'Custom' -CustomFormat 'dddd, MMMM dd, yyyy' # Returns: Monday, January 20, 2026 ``` ## Tests included Comprehensive Pester tests are included covering all format options to ensure reliable functionality.
1 parent f964789 commit 94e3625

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function Get-CurrentDateTime {
2+
<#
3+
.SYNOPSIS
4+
Returns the current date and time in a specified format.
5+
6+
.DESCRIPTION
7+
Returns the current date and time formatted according to the specified format string.
8+
Supports common format presets or custom format strings.
9+
10+
.EXAMPLE
11+
Get-CurrentDateTime
12+
13+
Returns the current date and time in the default format (yyyy-MM-dd HH:mm:ss).
14+
15+
.EXAMPLE
16+
Get-CurrentDateTime -Format 'Short'
17+
18+
Returns the current date in short date format.
19+
20+
.EXAMPLE
21+
Get-CurrentDateTime -Format 'Custom' -CustomFormat 'dddd, MMMM dd, yyyy'
22+
23+
Returns the current date in a custom format like "Monday, January 20, 2026".
24+
25+
.LINK
26+
https://MariusStorhaug.github.io/MariusTestModule/Functions/Get-CurrentDateTime/
27+
#>
28+
[OutputType([string])]
29+
[CmdletBinding()]
30+
param (
31+
# The format preset to use for the date and time output.
32+
[Parameter()]
33+
[ValidateSet('Default', 'Short', 'Long', 'ISO8601', 'Custom')]
34+
[string] $Format = 'Default',
35+
36+
# Custom format string when Format is set to 'Custom'.
37+
[Parameter()]
38+
[string] $CustomFormat = 'yyyy-MM-dd HH:mm:ss'
39+
)
40+
41+
$currentDateTime = Get-Date
42+
43+
switch ($Format) {
44+
'Default' {
45+
$currentDateTime.ToString('yyyy-MM-dd HH:mm:ss')
46+
}
47+
'Short' {
48+
$currentDateTime.ToShortDateString()
49+
}
50+
'Long' {
51+
$currentDateTime.ToLongDateString()
52+
}
53+
'ISO8601' {
54+
$currentDateTime.ToString('o')
55+
}
56+
'Custom' {
57+
$currentDateTime.ToString($CustomFormat)
58+
}
59+
}
60+
}

tests/PSModuleTest.Tests.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,29 @@ Describe 'Module' {
2121
It 'Function: Get-Greeting - Evening' {
2222
Get-Greeting -TimeOfDay 'Evening' | Should -Be 'Good Evening!'
2323
}
24+
25+
It 'Function: Get-CurrentDateTime - Default format' {
26+
$result = Get-CurrentDateTime
27+
$result | Should -Match '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'
28+
}
29+
30+
It 'Function: Get-CurrentDateTime - ISO8601 format' {
31+
$result = Get-CurrentDateTime -Format 'ISO8601'
32+
$result | Should -Match '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}'
33+
}
34+
35+
It 'Function: Get-CurrentDateTime - Custom format' {
36+
$result = Get-CurrentDateTime -Format 'Custom' -CustomFormat 'yyyy'
37+
$result | Should -Be (Get-Date).ToString('yyyy')
38+
}
39+
40+
It 'Function: Get-CurrentDateTime - Short format' {
41+
$result = Get-CurrentDateTime -Format 'Short'
42+
$result | Should -Not -BeNullOrEmpty
43+
}
44+
45+
It 'Function: Get-CurrentDateTime - Long format' {
46+
$result = Get-CurrentDateTime -Format 'Long'
47+
$result | Should -Not -BeNullOrEmpty
48+
}
2449
}

0 commit comments

Comments
 (0)