Public/Utils/ConvertTo-Date.ps1

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function ConvertTo-Date {

    <#
    .SYNOPSIS
    Converts a string to a DateTime using specified DateFormat.

    .DESCRIPTION
    See https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx for description of format strings.

    .EXAMPLE
    $date = ConvertTo-Date -String '2015-03-05' -DateFormat 'yyyy-MM-dd'
    #>


    [CmdletBinding()]
    [OutputType([DateTime])]
    param(
        # String to be converted to date.
        [Parameter(Mandatory = $false)]
        [string]
        $String,

        # Date formats that will be used to parse the string. If not specified, system default will be used.
        [Parameter(Mandatory = $false)]
        [string[]]
        $DateFormat,

        # If true, exception will be thrown if failed to convert string to date. Otherwise, $null will be returned.
        [Parameter(Mandatory = $false)]
        [switch]
        $ThrowOnFailure
    )

    if (!$String) {
        return $null
    }

    $success = $false

    if ($DateFormat) {
        [datetime]$result = New-Object -TypeName DateTime
        $success = [DateTime]::TryParseExact($String, $DateFormat, [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$result)
    } 
    else {
        [datetime]$result = New-Object -TypeName DateTime
        $success = [DateTime]::TryParse($String, [ref]$result)
    }

    if ($success) {
        return $result
    }

    if ($ThrowOnFailure) {
        throw "Failed to convert string '$String' to date using formats $DateFormat."
    }

    return $null
}