Last active
March 20, 2023 05:04
-
-
Save rchaganti/9055e80c1995e112945c07c43e3be33d to your computer and use it in GitHub Desktop.
tzDiff.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param | |
( | |
[Parameter(Mandatory = $true)] | |
[String[]] | |
$Timezone, | |
[Parameter()] | |
[Datetime] | |
$Target | |
) | |
if (!$Target) | |
{ | |
# Get the local time as the target | |
$Target = Get-Date | |
} | |
$tzObject = [System.Collections.Arraylist]::new() | |
$tzObject.Add( | |
[PSCustomObject]@{ | |
'Timezone' = 'Local' | |
'Time' = $Target | |
'DifferenceInHours' = 0 | |
} | |
) | Out-Null | |
foreach ($tz in $Timezone) | |
{ | |
# Get the timezone difference | |
$localTz = [System.TimeZoneInfo]::Local | |
$remoteTz = [System.TimeZoneInfo]::FindSystemTimeZoneById($tz) | |
$tzDifference = [float]($remoteTz.BaseUtcOffset.TotalHours - $localTz.BaseUtcOffset.TotalHours) | |
$remoteTime = [System.TimeZoneInfo]::ConvertTime($Target, $localTz, $remoteTz) | |
$tzObject.Add( | |
[PSCustomObject]@{ | |
'Timezone' = $tz | |
'Time' = $remoteTime | |
'DifferenceInHours' = $tzDifference | |
} | |
) | Out-Null | |
} | |
return $tzObject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment