Getting Timezones

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

Here's a low level call that returns all time zones:

PS> [System.TimeZoneInfo]::GetSystemTimeZones()

Id                         : Dateline Standard Time
DisplayName                : (UTC-12:00) International Date Line West
StandardName               : Dateline Standard Time
DaylightName               : Dateline Daylight Time
BaseUtcOffset              : -12:00:00
SupportsDaylightSavingTime : False

Id                         : UTC-11
DisplayName                : (UTC-11:00) Coordinated Universal Time-11
StandardName               : UTC-11
DaylightName               : UTC-11
BaseUtcOffset              : -11:00:00
SupportsDaylightSavingTime : False
(...)

To find the time zone you are in, use this:

PS> [System.TimeZoneInfo]::Local

Id                         : W. Europe Standard Time
DisplayName                : (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
StandardName               : W. Europe Standard Time
DaylightName               : W. Europe Daylight Time
BaseUtcOffset              : 01:00:00
SupportsDaylightSavingTime : True

PS> ([System.TimeZoneInfo]::Local).StandardName
W. Europe Standard Time

Twitter This Tip! ReTweet this Tip!


Posted Mar 26 2012, 06:00 AM by ps1

Comments

Brent Challis wrote re: Getting Timezones
on 03-26-2012 10:02 PM

Nice simple use of the framework.  it gave me the idea of wrapping it up in a function so that you could use it to get the time zone for a specific city:

function Get-TimeZone

{

   Param

   (

       [string]$City

   )

   $timeZones = [System.TimeZoneInfo]::GetSystemTimeZones()

   foreach ($timeZone in $timeZones)

   {

       if ($City -eq $null -or ($timeZone -like ("*"+$City+"*")))

       {

           Write-Output $timeZone

       }

   }

}

Silence wrote re: Getting Timezones
on 09-21-2014 9:15 PM

Convert the current time to different time zone:

$now = Get-Date

[System.TimeZoneInfo]::GetSystemTimeZones() | ForEach-Object{

   Write-Host ("{0,-35} ==>> {1}" -f $_.Id,[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($now, $_.Id))

}

Copyright 2012 PowerShell.com. All rights reserved.