Functions/Get-TruncatedDate.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function Get-TruncatedDate {
<#
.SYNOPSIS
    To truncate a date at a given level
.DESCRIPTION
    To truncate a date at a given level
.PARAMETER Date
    The date that you wish to truncate.
.PARAMETER WhereToTruncate
    A string containing where in the date you wish to truncate. Has a ValidateSet
    against it of: 'Millisecond', 'Second', 'Minute', 'Hour', 'Day', 'Month'
.PARAMETER IncludeInput
    A switch determining if you wish to see the original date in the output, aliased to 'IncludeOriginal'
.EXAMPLE
    All of the following examples will be looking to truncate $testdate. Here is how $testdate
    is originally set.
 
    $prop = ([ordered] @{
        Year = 2020
        Month = 3
        Day = 15
        Hour = 11
        Minute = 39
        Second = 43
        Millisecond = 780
    })
    $testdate = get-date @prop
.EXAMPLE
    Example of truncating at Millisecond and including original date in output
 
    Get-TruncatedDate -Date $testdate -WhereToTruncate Millisecond -IncludeInput
 
    Original Where Truncated
    -------- ----- ---------
    2020/03/15 11:39:43.780 MilliSecond 2020/03/15 11:39:43.000
.EXAMPLE
    Example of truncating at Second
    Get-TruncatedDate -Date $testdate -WhereToTruncate Second
 
    Sunday, March 15, 2020 11:39:00 AM
.EXAMPLE
    Example of truncating at Minute
    Get-TruncatedDate -Date $testdate -WhereToTruncate Minute
 
    Sunday, March 15, 2020 11:00:00 AM
.EXAMPLE
    Example of truncating at Hour
    Get-TruncatedDate -Date $testdate -WhereToTruncate Hour
 
    Sunday, March 15, 2020 12:00:00 AM
.EXAMPLE
    Example of truncating at Day
    Get-TruncatedDate -Date $testdate -WhereToTruncate Day
 
    Sunday, March 1, 2020 12:00:00 AM
.EXAMPLE
    Example of truncating at Month
    Get-TruncatedDate -Date $testdate -WhereToTruncate Month
 
    Wednesday, January 1, 2020 12:00:00 AM
.NOTES
    # source # https://www.reddit.com/r/usefulscripts/comments/9ghdzo/powershell_setdatetruncate_is_that_dumb_function/
 
#>


    [CmdletBinding(ConfirmImpact = 'None')]
    Param
    (
        [Parameter(Position = 0, ValueFromPipeline)]
        [datetime[]] $Date = $(Get-Date),

        [ValidateSet('Millisecond', 'Second', 'Minute', 'Hour', 'Day', 'Month')]
        [string] $WhereToTruncate = 'Hour',

        [Alias('IncludeOriginal')]
        [switch] $IncludeInput
    )

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
    }

    process {
        switch ($WhereToTruncate) {
            'MilliSecond' {
                $Where = 'MilliSecond'
                $GD_Params = @{
                    MilliSecond = 0
                }
                break
            }
            'Second' {
                $Where = 'Second'
                $GD_Params = @{
                    MilliSecond = 0
                    Second      = 0
                }
                break
            }
            'Minute' {
                $Where = 'Minute'
                $GD_Params = @{
                    MilliSecond = 0
                    Second      = 0
                    Minute      = 0
                }
                break
            }
            'Hour' {
                $Where = 'Hour'
                $GD_Params = @{
                    MilliSecond = 0
                    Second      = 0
                    Minute      = 0
                    Hour        = 0
                }
                break
            }
            'Day' {
                $Where = 'Day'
                $GD_Params = @{
                    MilliSecond = 0
                    Second      = 0
                    Minute      = 0
                    Hour        = 0
                    Day         = 1
                }
                break
            }
            'Month' {
                $Where = 'Month'
                $GD_Params = @{
                    MilliSecond = 0
                    Second      = 0
                    Minute      = 0
                    Hour        = 0
                    Day         = 1
                    Month       = 1
                }
                break
            }
        } # end >> switch ($WhereToTruncate)

        foreach ($currentDate in $Date) {
            $ReturnVal = $currentDate | Get-Date @GD_Params
            if ($IncludeInput) {
                New-Object -TypeName 'psobject' -Property ([ordered] @{
                        Original  = Get-Date -Date $currentDate -Format 'yyyy/MM/dd HH:mm:ss.fff'
                        Where     = $Where
                        Truncated = Get-Date -Date $ReturnVal   -Format 'yyyy/MM/dd HH:mm:ss.fff'
                    })
            } else {
                Write-Output -InputObject $ReturnVal
            }
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }

}