Converting Times from One Time Zone to Another Time Zone in PowerShell

I was doing some work with Service Manager and Orchestrator over the last few weeks and ran into a situation where I had to convert a time in a database to another time zone in PowerShell. I found lots of examples on doing this from the current time to another time zone but I didn’t run across anything that converted a datetime that isn’t current from one time zone (not necessarily where I’m running the script from) to another time zone. I decided to write something up and figured it might be useful for the community.

This script has three different modes it can run in.

  1. You can run the script with no parameters. In this case it takes your current time and time zone and displays the conversion in a table for every other time zone. This is similar to other examples you’ll find online.
  2. You can run the script with the –time and –fromTimeZone parameters filled in. This takes the time and time zone you passed in and displays the converted time for every other time zone in a table.
  3. You can also run the script with the –time, –fromTimeZone, and –toTimeZone parameters filled in. This does the same as the example above but only displays the converted time for the time zone specified in the toTimeZone parameter.

Examples

  • Display the current time/time zone and a table with your local time converted to every other time zone. You can also use this to get all the time zone id’s that you can use as parameters in the other two modes of running this script.
    • .\ConvertTimeZones.ps1
  • Display the passed in time/time zone and a table with that time converted to every other time zone.
    • .\ConvertTimeZones.ps1 –time “2014-07-29T03:15:00” –fromTimeZone “Central Standard Time”
  • Display the passed in time/time zone and the converted time to one other time zone.
    • .\ConvertTimeZones.ps1 –time “2014-07-29T03:15:00” –fromTimeZone “West Pacific Standard Time” –toTimeZone “Eastern Standard Time”

 

 param(
  [Parameter(Mandatory=$false)]
  $time,
  [Parameter(Mandatory=$false)]
  $fromTimeZone,
  [Parameter(Mandatory=$false)]
  $toTimeZone
)

function ConvertTime
{
  param($time, $fromTimeZone, $toTimeZone)

  $oFromTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($fromTimeZone)
  $oToTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($toTimeZone)
  $utc = [System.TimeZoneInfo]::ConvertTimeToUtc($time, $oFromTimeZone)
  $newTime = [System.TimeZoneInfo]::ConvertTime($utc, $oToTimeZone)

  return $newTime
}

function ConvertUTC
{
  param($time, $fromTimeZone)

  $oFromTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($fromTimeZone)
  $utc = [System.TimeZoneInfo]::ConvertTimeToUtc($time, $oFromTimeZone)
  return $utc
}

if ($toTimeZone)
{
  
  [datetime]$time = $time
  $toUTC = ConvertUTC -time $time -fromTimeZone $fromTimeZone
  $toNewTimeZone = ConvertTime -time $time -fromTimeZone $fromTimeZone -toTimeZone $toTimeZone
  Write-Host ("Original Time ({0}): {1}" -f $fromTimeZone, $time)
  Write-Host ("UTC Time: {0}" -f $toUTC)
  Write-Host ("{0}: {1}" -f $toTimeZone, $toNewTimeZone)
}
else
{
  if (!($time)) 
  {
    $fromTimeZone = (([System.TimeZoneInfo]::Local).Id).ToString()
    $time = [DateTime]::SpecifyKind((Get-Date), [DateTimeKind]::Unspecified)
  }
  else { [datetime]$time = $time }
  Write-Host ("Original Time - {0}: {1}" -f $fromTimeZone, $time)
  $toUTC = ConvertUTC -time $time -fromTimeZone $fromTimeZone
  $times = @()
  foreach ($timeZone in ([system.timezoneinfo]::GetSystemTimeZones()))
  {
   $times += (New-Object psobject -Property @{'Name' = $timeZone.DisplayName; 'ID' = $timeZone.id; 'Time' = (ConvertTime -time $time -fromTimeZone $fromTimeZone -toTimeZone $timeZone.id); 'DST' = $timeZone.SupportsDaylightSavingTime})
  }
  $times | Sort-Object Time | Format-Table -Property * -AutoSize
}

ConvertTimeZones.renametops1