Public/ConvertFrom-ALHDateTime.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
<#PSScriptInfo
 
.VERSION 1.1.0
 
.GUID 15cfc452-2c60-428e-9a06-1fc3bd4e9f9f
 
.AUTHOR Dieter Koch
 
.COMPANYNAME
 
.COPYRIGHT (c) 2021-2023 Dieter Koch
 
.TAGS
 
.LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE
 
.PROJECTURI https://github.com/admins-little-helper/ALH
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
    1.0.0
    Initial release
 
    1.1.0
    Added pipeline support. Set defaut value to current date/time for parameter 'DateTime'
 
#>



<#
 
.DESCRIPTION
Contains a function to convert a local date/time to miliseconds since 1970 or milliseconds since 1601 or ticks since 1601.
 
.LINK
https://github.com/admins-little-helper/ALH
 
.LINK
https://docs.microsoft.com/en-US/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties
 
#>



function ConvertFrom-ALHDateTime {
    <#
    .SYNOPSIS
    Converts a local date/time to miliseconds since 1970 or milliseconds since 1601 or ticks since 1601.
 
    .DESCRIPTION
    Converts a local date/time to miliseconds since 1970 or milliseconds since 1601 or ticks since 1601.
 
    .PARAMETER DateTime
    Date to convert. Defaults to current date/time.
 
    .EXAMPLE
    ConvertFrom-ALHDateTime
 
    DateTime MillisecondsSince1970 MillisecondsSince1601 TicksSince1601
    -------- --------------------- --------------------- --------------
    11.11.2022 21:14:49 1668201289484 13312674889484 133126748894840000
 
    This example shows how to convert the current date/time to MillisecondsSince1970, MillisecondsSince1601 and TicksSince1601.
 
    .EXAMPLE
    ConvertFrom-ALHDateTime -DateTime "11.11.2011 11:11:11"
 
    DateTime MillisecondsSince1970 MillisecondsSince1601 TicksSince1601
    -------- --------------------- --------------------- --------------
    11.11.2011 11:11:00 1321009860000 12965483460000 129654834600000000
 
    This example shows how to convert a date as string to MillisecondsSince1970, MillisecondsSince1601 and TicksSince1601.
    Note that in this example PowerShell automatically casts the String value to a DateTime value. Use this carefully to make sure the
    date/time format matches your local date time format.
 
    .INPUTS
    System.DateTime
 
    .OUTPUTS
    PSCustomObject
 
    .NOTES
    Author: Dieter Koch
    Email: diko@admins-little-helper.de
 
    .LINK
    https://github.com/admins-little-helper/ALH/blob/main/Help/ConvertFrom-ALHDateTime.txt
    #>


    [cmdletbinding()]
    param
    (
        [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [DateTime[]]
        $DateTime = @(, (Get-Date))
    )

    begin {
        $DT1970 = Get-Date -Date "1970-01-01T00:00:00"
        $DT1601 = Get-Date -Date "1601-01-01T00:00:00"
    }

    process {
        foreach ($DateTimeElement in $DateTime) {
            try {
                [int64]$MillisecondsSince1970 = ($DateTimeElement - $DT1970).TotalMilliseconds
                [int64]$MillisecondsSince1601 = ($DateTimeElement - $DT1601).TotalMilliseconds
            }
            catch {
                $_
            }

            $DateTimeConverted = [PSCustomObject]@{
                DateTime              = $DateTimeElement
                MillisecondsSince1970 = $MillisecondsSince1970
                MillisecondsSince1601 = $MillisecondsSince1601
                TicksSince1601        = $MillisecondsSince1601 * 10000
            }

            $DateTimeConverted
        }
    }
}


#region EndOfScript
<#
################################################################################
################################################################################
#
# ______ _ __ _____ _ _
# | ____| | | / _| / ____| (_) | |
# | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_
# | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __|
# | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_
# |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__|
# | |
# |_|
################################################################################
################################################################################
# created with help of http://patorjk.com/software/taag/
#>

#endregion