-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-DateTimeFromString.ps1
More file actions
45 lines (40 loc) · 1.45 KB
/
Get-DateTimeFromString.ps1
File metadata and controls
45 lines (40 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<#
.SYNOPSIS
Convert the date-time string into the DateTime.
.DESCRIPTION
Convert the date-time string into the DateTime. However, Parse the date from the $String and $Format and return DateTime. Note that, return $null if $String or $Format is not valid.
.PARAMETER String*
Required. String must be date-time string with the following of the $Format. Return $null if the $Format is not valid.
.PARAMETER Format
Optional. $Format must be the following of the $String string datetime format. Default format is 'dd MMMM yyyy hh:mm:ss'
.OUTPUTS
Return DateTime or $null if $String or $Format is not valid.
.EXAMPLE
Get-DateTimeFromString -String '2010-12-12' -Format 'yyyy-MM-dd'
Get-DateTimeFromString -String '10-2020' -Format 'MM-yyyy'
Get-DateTimeFromString -String '31 January 2020 15:39:43'
#>
function Get-DateTimeFromString() {
[cmdletbinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$String,
[string]$Format
)
Begin {
if ($Format.Length -le 0) {
$Format = $Global:Resources.DefaultDateTimeFormat;
}
}
Process {
try {
return [datetime]::ParseExact($String, $Format, $null);
}
catch {
Write-Exception -ErrorObj $_ -Message "Throws an exception in 'Get-DateTimeFromString'" -Stop $false
return $null;
}
}
End {
}
}