Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

It’s About Time

Posted on October 24, 2020 by Alan

I recently went down the rabbit hole working with PowerShell, datetime and time zone calculations.  Here are some of the things that I learned:

          1. Use [datetimeoffset] instead of [datetime]. [datetimeoffset]  has the daylight savings offset as part of the object. Example:
            [datetimeoffset]$ThisDate = Get-Date -Year 2007 -Month 3 -Day 10  -Hour 0 -Minute 0 -Second 0
          2.  The best way to convert AD times such LastLogonTimeStamp is to use the FromFileTime method:
            #Convert LLTs to date
            $RawLLTS = 132467180950915594
            $oLLTS = [System.DateTimeOffset]::FromFileTime($Rawllts)
            $oLLTS
          3. Getting a list of all TimeZones is easy:
            Function ShowPossibleTimeZones($offsetTime) {
                $offset = $offsetTime.offset
                Write-Host ("{0} could belong to the following time zones:" -f $offsetTime.ToString());
                # Get all time zones defined on local system
                $timeZones = [TimeZoneInfo]::GetSystemTimeZones()
            
                # Iterate time zones
                foreach ($timeZone in $timeZones) {
                    # Compare offset with offset for that date in that time zone
                    if ($timeZone.GetUtcOffset(($offsetTime.DateTime)) -eq $offset ) {
                        Write-Host ("   {0}" -f $timeZone.DisplayName);
                    }
                }
            }
          4. Converting between time zones is also pretty easy. Here is the list of time zone formats for .NET.
            #Zone ID is standard name, $tf is a dot net time format
            Function Convert-TimeToAnotherZone ($dt, $ZoneID,$tf){
                $Date = [System.DateTimeOffset]::Parse($dt)
                $objDate = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($Date,$ZoneID)
            
                if ($null -eq $tf ){$tf = 'd'}
                $objDate.DateTime.IsDaylightSavingTime() | Set-Variable isDT
                if ($isDT){
                    $zone = ([System.TimeZoneInfo]::FindSystemTimeZoneById($ZoneID)).DaylightName
                }Else{
                    $zone = $TZID
                }
                $objDate.DateTime.tostring($tf) + " $zone"
            } 
            
            #Convert date to another time zone by zone name
            $testDate = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($ThisDate,'Central Standard Time')
            Convert-TimeToAnotherZone -dt $testDate -ZoneID 'Mountain Standard Time' -tf 'G'
            
          5. Putting this together makes it easy to know what the time is in another time zone:
            [System.DateTimeOffset]$now  = Get-Date
            $PT = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($Now,'Pacific Standard Time')
            Convert-TimeToAnotherZone -dt $pt -ZoneID 'Mountain Standard Time' -tf 'G'

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy