-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-StringFromDateTime.ps1
More file actions
43 lines (38 loc) · 1.17 KB
/
Get-StringFromDateTime.ps1
File metadata and controls
43 lines (38 loc) · 1.17 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
<#
.SYNOPSIS
Convert the DateTime into the string.
.DESCRIPTION
Convert the DateTime into the string based on the $Date and $Format. Note that, return $null if $Date or $Format is not valid.
.PARAMETER Date*
Required. Date must be valid DateTime. Return $null if the $Format is not valid.
.PARAMETER Format
Optional. Date format string. If $Format is $null, convert with the default DateTime string format.
.OUTPUTS
Return DateTime or $null if $Date or $Format is not valid.
.EXAMPLE
Get-StringFromDateTime '12 December 2010 00:00:00'
#>
function Get-StringFromDateTime() {
[cmdletbinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[datetime]$Date,
[string]$Format
)
Begin {
if ($Format.Length -le 0) {
$Format = $Global:Resources.DefaultDateTimeFormat;
}
}
Process {
try {
return ([datetime]$Date).ToString($Format);
}
catch {
Write-Exception -ErrorObj $_ -Message "Throws an exception in 'Get-StringFromDateTime'" -Stop $true
return $null;
}
}
End {
}
}