Created
February 11, 2018 20:12
-
-
Save MSAdministrator/b2c3ace163c4fbf76069503528de6cf1 to your computer and use it in GitHub Desktop.
Confirm if a date time format pattern is valid or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Confirm if a date time format pattern is valid or not | |
.DESCRIPTION | |
Confirm if a date time format pattern is valid or not | |
based on the current culture on the machine that this function | |
is being ran on | |
.EXAMPLE 1 | |
PS C:\> Confirm-DateTimeFormatPattern -Format "yyyy-MM-dd HH:mm:ss" | |
Format Valid | |
------ ----- | |
yyyy-MM-dd HH:mm:ss True | |
.EXAMPLE 2 | |
PS C:\> Confirm-DateTimeFormatPattern -Format "yyyy-MM-dd HH:mm:ss", "xxx-MM/dd/yy" | |
Format Valid | |
------ ----- | |
yyyy-MM-dd HH:mm:ss True | |
xxx-MM/dd/yy False | |
.EXAMPLE 3 | |
PS C:\> "yyyy-MM-dd HH:mm:ss", "xxx-MM/dd/yy", "MMMM d", "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK" | Confirm-DateTimeFormatPattern | |
Format Valid | |
------ ----- | |
yyyy-MM-dd HH:mm:ss True | |
xxx-MM/dd/yy False | |
MMMM d True | |
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK True | |
#> | |
function Confirm-DateTimeFormatPattern | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([PSCustomObject])] | |
Param | |
( | |
# Param1 help description | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
[string[]]$Format | |
) | |
Begin | |
{ | |
$CultureObject = ([System.Globalization.DateTimeFormatInfo]::CurrentInfo).GetAllDateTimePatterns() | |
} | |
Process | |
{ | |
foreach ($item in $Format) | |
{ | |
if ($CultureObject -contains $item) | |
{ | |
$props = [PSCustomObject]@{ | |
'Format' = $item | |
'Valid' = $true | |
} | |
Write-Output $props | |
} | |
else | |
{ | |
$props = [PSCustomObject]@{ | |
'Format' = $item | |
'Valid' = $false | |
} | |
Write-Output $props | |
} | |
} | |
} | |
End | |
{ | |
# Intentionally Left Blank | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment