Skip to content

PowerShell··1 min read

TimeZone

Sometimes you have users in a ton of different time zones. This powershell function is something I use to better help me understand what time it is for my team members around the world.

TimeZone

When you work at a global company, sometimes it’s helpful to convert a time into all of your applicable time zones. However, the larger your company get’s the harder this task becomes to do manually.

One day, I got annoyed, and decided to just write some code, to handle it all for me. I also added some features where it will round the time, and convert the output to markdown for you.

function Get-StandardTimes {
<#
.Description
The Get-StandardTimes function is designed to get a bundle of standardized times from various time zones you care about.
To update the list of time zones you care about update the array of time zones with the results from "Get-TimeZone".

.EXAMPLE
Get-StandardTimes

Time                   Zone
----                   ----
8/30/2022 11:43:33 AM  Pacific Standard Time
8/30/2022 12:43:33 PM  Mountain Standard Time
8/30/2022 1:43:33 PM   Central Standard Time
8/30/2022 2:43:33 PM   Eastern Standard Time
8/30/2022 6:43:33 PM   UTC
8/30/2022 7:43:33 PM   GMT Standard Time
8/30/2022 8:43:33 PM   Central European Standard Time
8/30/2022 8:43:33 PM   Romance Standard Time
8/30/2022 8:43:33 PM   W. Europe Standard Time
8/31/2022 12:13:33 AM  India Standard Time
8/31/2022 3:43:33 AM   Tokyo Standard Time
8/31/2022 4:43:33 AM   AUS Eastern Standard Time

Note how the time zones are output in a simple standard time with the current exact time.

.EXAMPLE
Get-StandardTimes -AsMarkdown

Time                  | Zone
--------------------- | ------------------------------
8/30/2022 11:44:28 AM | Pacific Standard Time
8/30/2022 12:44:28 PM | Mountain Standard Time
8/30/2022 1:44:28 PM  | Central Standard Time
8/30/2022 2:44:28 PM  | Eastern Standard Time
8/30/2022 6:44:28 PM  | UTC
8/30/2022 7:44:28 PM  | GMT Standard Time
8/30/2022 8:44:28 PM  | Central European Standard Time
8/30/2022 8:44:28 PM  | Romance Standard Time
8/30/2022 8:44:28 PM  | W. Europe Standard Time
8/31/2022 12:14:28 AM | India Standard Time
8/31/2022 3:44:28 AM  | Tokyo Standard Time
8/31/2022 4:44:28 AM  | AUS Eastern Standard Time

Returns the times of various time zones in standard Markdown format.

.EXAMPLE
Get-StandardTimes -Ish

Time                   Zone
----                   ----
8/30/2022 11:46:00 AM  Pacific Standard Time
8/30/2022 12:46:00 PM  Mountain Standard Time
8/30/2022 1:46:00 PM   Central Standard Time
8/30/2022 2:46:00 PM   Eastern Standard Time
8/30/2022 6:46:00 PM   UTC
8/30/2022 7:46:00 PM   GMT Standard Time
8/30/2022 8:46:00 PM   Central European Standard Time
8/30/2022 8:46:00 PM   Romance Standard Time
8/30/2022 8:46:00 PM   W. Europe Standard Time
8/31/2022 12:16:00 AM  India Standard Time
8/31/2022 3:46:00 AM   Tokyo Standard Time
8/31/2022 4:46:00 AM   AUS Eastern Standard Time

Rounds the tiem zone object down to the nearest minute.

.EXAMPLE
Get-StandardTimes -Time $(Get-Date -hour 7)

Produces the same output but for Hour 7 in your local time zone.
#>
    [CmdletBinding()]
    param (
        [Parameter(HelpMessage = "Provide a time to convert to common time zones." , Mandatory = $FALSE,ValueFromPipeline = $true )]
        [datetime]$Time = $(Get-Date),
        [Parameter(HelpMessage = "Provide the output as Markdown" , Mandatory = $FALSE)]
        [switch]$AsMarkdown,
        [Parameter(HelpMessage = "Specify Accuracy" , Mandatory = $FALSE)]
        [switch]$Ish
    )
    begin{
        Function ConvertTo-Markdown {
            [CmdletBinding()]
            [OutputType([string])]
            Param (
                [Parameter(
                    Mandatory = $true,
                    Position = 0,
                    ValueFromPipeline = $true
                )]
                [PSObject[]]$InputObject
            )
            Begin {
                $items = @()
                $columns = @{}
            }
            Process {
                ForEach($item in $InputObject) {
                    $items += $item
                    $item.PSObject.Properties | %{
                        if($_.Value -ne $null){
                            if(-not $columns.ContainsKey($_.Name) -or $columns[$_.Name] -lt $_.Value.ToString().Length) {
                                $columns[$_.Name] = $_.Value.ToString().Length
                            }
                        }
                    }
                }
            }
            End {
                ForEach($key in $($columns.Keys)) {
                    $columns[$key] = [Math]::Max($columns[$key], $key.Length)
                }
                $header = @()
                ForEach($key in $columns.Keys) {
                    $header += ('{0,-' + $columns[$key] + '}') -f $key
                }
                $header -join ' | '
                $separator = @()
                ForEach($key in $columns.Keys) {
                    $separator += '-' * $columns[$key]
                }
                $separator -join ' | '
                ForEach($item in $items) {
                    $values = @()
                    ForEach($key in $columns.Keys) {
                        $values += ('{0,-' + $columns[$key] + '}') -f $item.($key)
                    }
                    $values -join ' | '
                }
            }
        }
    }
    process{
        $localTimeZone = $(Get-TimeZone).id
        $alternateTimeZones = @("Pacific Standard Time","Mountain Standard Time","Central Standard Time","Eastern Standard Time","UTC","GMT Standard Time","Central European Standard Time","Romance Standard Time","W. Europe Standard Time","India Standard Time","Tokyo Standard Time","AUS Eastern Standard Time")
        if($ish.IsPresent){
            $time = Get-Date $time -Millisecond 0 -Second 0
        }
        $times = foreach($timeZone in $alternateTimeZones){
            [PSCustomObject]@{
                Time = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($Time,"$timeZone")
                Zone = $timeZone
            }
        }
        if($AsMarkdown.IsPresent){
            $times = ConvertTo-Markdown -InputObject $times
            return $times
        }
        else{
            return $times
        }
    }
}

Comments

Comments are GitHub Discussions, loaded on request.