Skip to main content

Get a US Navy Date Time Group (DTG) with PowerShell

This past week I’ve been at Naval Station Great Lakes training for a mission later this year.  As I’m training up for this mission, I’m seeing some procedures that may benefit from PowerShell.  Many of the procedures that I will be executing involves utilizing a Navy Date Time Group (DTG).  The problem with DTGs is that they are not exactly logical.  Here is the format.
2 digit day
2 digit hour
2 digit minute
Z for ZUL (GMT for the civilians out there)
3 Character month
2 digit year.
So, the procedure that I will be utilizing this code with will require that I either figure this out on my own under a high tempo/stress situation, or I simply ask PowerShell to do this.  Since we do not utilize PowerShell for any of our tasks, I also needed a way to copy and paste this information into an application.  The code below will generate the DTG and also place it on the clipboard.  Relying on a manual copy and paste or simply getting the DTG from PowerShell and manually typing it would have added up to a lot of wasted time and potential errors.  To send any PowerShell output to the clipboard, simply pipe it to Clip. For Example:
Get-Service | Clip
Since the default need for this code is to place it on the clip board and not the in the pipeline, A –PassThru parameter is provided that will suppress all screen output and send a System.DateTime object with the DTG property to the pipeline.

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
Function Get-NAVYDTG
{
[CmdletBinding()]
Param([Switch]$NoClip,
      [Switch]$PassThru
)
    $Date = (Get-Date).ToUniversalTime()
 
    # Day Component
    Switch ($Date.day.ToString().length)
    {
        1 {$Day = "0$($Date.Day)"; BREAK}
        Default {$Day = "$($Date.Day)"}
    }

    # Hour Component
    Switch ($Date.Hour.ToString().length)
    {
        1 {$Hour = "0$($Date.Hour)"; BREAK}
        Default {$Hour = "$($Date.Hour)"}
    }

    # Minute Component
    Switch ($Date.Minute.ToString().length)
    {
        1 {$Minute = "0$($Date.Minute)"; BREAK}
        Default {$Minute = "$($Date.Minute)"}
    }

    # Month Component
    $Month = ((((get-date).
          GetDateTimeFormats())[6]).
                   remove("0","3")).
                          Remove(3).
                          ToUpper()

    # Year Component
    $Year = ((get-date).Year.toString().Remove("0","2"))
 
    If ((!$NoClip) -and (!$PassThru)
)
    {
        Write-Output "$($Day)$($Hour)$($Minute)Z$($Month)$($Year)" |
        Clip
        Write-Host "$($Day)$($Hour)$($Minute)Z$($Month)$($Year)"
        Write-Host "Sent to Clipboard"
    }
    ELSEIF ($PassThru)
    {
        $Date |
        Select-Object -Property *,
        @{Name="DTG";Expression={"$($Day)$($Hour)$($Minute)Z$($Month)$($Year)"}} |
        Write-Output
    }
    Else
    {
        Write-Output "$($Day)$($Hour)$($Minute)Z$($Month)$($Year)"
    }

<#
.SYNOPSIS
Returns a Navy Date Time Group (DTG)

.DESCRIPTION
Returns a Navy Date Group Group (DTG) and copies it to the clipboard.

.PARAMETER NoClip
Prevents the DTG value from being sent to the clipboard and only
displayed on the screen.

.PARAMETER PassThru
Suppresses all screen output and passes a full Systsem.DateTime object
to the pipeline with the DTG property added.

.EXAMPLE
Get-NAVYDTG
040219ZAUG13
Sent to Clipboard

Displays the DTG and sends it to the clipboard

.EXAMPLE
Get-NAVYDTG -NoClip
040220ZAUG13

Displays the DTG, but dose not copy it to the clipboard.

.EXAMPLE
Get-NAVYDTG -PassThru
 
DateTime    : Sunday, May 05, 2013 7:42:17 PM
Date        : 5/5/2013 12:00:00 AM
Day         : 5
DayOfWeek   : Sunday
DayOfYear   : 125
Hour        : 19
Kind        : Utc
Millisecond : 446
Minute      : 42
Month       : 5
Second      : 17
Ticks       : 635033797374465212
TimeOfDay   : 19:42:17.4465212
Year        : 2013
DTG         : 051942ZAUG13

Passes a System.DateTime object to the pipeline with the DTG
property added.
#>
} # End Function Get-NAVYDTG

We represent the DTG as ZULU or GMT time.  Line 7 converts the local into ZULU time.
Since a proper DTG must have 2 digit representation of the day,hour, and minute components, I needed a way to add a zero to any one of these components should they return a single digit. Lines 9 through 28 provides this assurance.
The month component must be given with only 3 characters and must be represented in upper case.  Line 31 provides this.
Line 38 provides the last 2 digits of the year.
Lines 40-47 send the properly formatted DTG to the clipboard if the –NoClip and –PassThru parameters are omitted. 
If –PassThru is provided, lines 48-54 send the System.DateTime object with a DTG property added to the pipeline.
If –NoClip is provided, line 57 simply writes the DTG to the pipeline.








Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.