0 out of 17 rated this helpful Rate this topic

TimeZoneInfo.FindSystemTimeZoneById Method

Retrieves a TimeZoneInfo object from the registry based on its identifier.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static TimeZoneInfo FindSystemTimeZoneById(
	string id
)

Parameters

id
Type: System.String
The time zone identifier, which corresponds to the Id property.

Return Value

Type: System.TimeZoneInfo
An object whose identifier is the value of the id parameter.
Exception Condition
OutOfMemoryException

The system does not have enough memory to hold information about the time zone.

ArgumentNullException

The id parameter is Nothing.

TimeZoneNotFoundException

The time zone identifier specified by id was not found. This means that a registry key whose name matches id does not exist, or that the key exists but does not contain any time zone data.

SecurityException

The process does not have the permissions required to read from the registry key that contains the time zone information.

InvalidTimeZoneException

The time zone identifier was found, but the registry data is corrupted.

The id parameter must correspond exactly to the time zone's registry key in length, but not in case, for a successful match to occur; that is, the comparison of id with time zone identifiers is case-insensitive. If you want to retrieve time zone objects based on partial matches, you can write custom procedures that work with the read-only collection of TimeZoneInfo objects returned by the GetSystemTimeZones method.

FindSystemTimeZoneById tries to match id to the subkey names of the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones branch of the registry under Windows XP and Windows Vista. This branch does not necessarily contain a comprehensive list of time zone identifiers. If required by an application, you can create a particular time zone either by calling one of the overloads of the CreateCustomTimeZone method or by calling FromSerializedString to deserialize a TimeZoneInfo object that represents the required time zone. However, time zones created by these method calls are not included in the registry and cannot be retrieved using the FindSystemTimeZoneById method. These custom time zones can be accessed only through the object reference returned by the CreateCustomTimeZone or FromSerializedString method call.

The following example uses the FindSystemTimeZoneById method to retrieve the Tokyo Standard Time zone. This TimeZoneInfo object is then used to convert the local time to the time in Tokyo and to determine whether it is Tokyo Standard Time or Tokyo Daylight Time.


// Get time in local time zone 
DateTime thisTime = DateTime.Now;
Console.WriteLine("Time in {0} zone: {1}", TimeZoneInfo.Local.IsDaylightSavingTime(thisTime) ?
                  TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName, thisTime);
Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local));
// Get Tokyo Standard Time zone
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst);      
Console.WriteLine("Time in {0} zone: {1}", TimeZoneInfo.Local.IsDaylightSavingTime(tstTime) ?
                  tst.DaylightName : tst.StandardName, tstTime);
Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));


.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
FindSystemTimeZoneById Sample using PowerShell
<#
.SYNOPSIS
This script converts time to different time zones.
.DESCRIPTION
This script reimplements an MSDN sample using PowerShell
.NOTES
File Name : Get-Time.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.findsystemtimezonebyid.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-Time.ps1
Time in GMT Daylight Time zone: 8/29/2010 11:40:50 AM
UTC Time: 8/29/2010 10:40:50 AM
Time in Tokyo Daylight Time zone: 8/29/2010 07:40:50 PM
UTC Time: 8/29/2010 10:40:50 AM
#>

# Get local time
$thisTime = [system.DateTime]::Now

if ([System.TimeZoneInfo]::Local.IsDaylightSavingTime($thisTime)) {
$tzn = [System.TimeZoneInfo]::Local.DaylightName }
else {
$tzn = [System.TimeZoneInfo]::Local.StandardName
}
# Display local Time
"Time in {0} zone: {1}" -f $tzn, $thisTime
" UTC Time: {0}" -f [system.TimeZoneInfo]::ConvertTimeToUtc($thisTime, [TimeZoneInfo]::Local)

# Get Tokyo Standard Time zone
$tst = [system.TimeZoneInfo]::FindSystemTimeZoneById("Tokyo Standard Time")
$tstTime = [system.TimeZoneInfo]::ConvertTime($thisTime, [TimeZoneInfo]::local, $tst)
$tstzn = if ( [System.TimeZoneInfo]::Local.IsDaylightSavingTime($tstTime)) {
$tst.DaylightName} else {$tst.StandardName}

# Display Tokyo Time Zone
"Time in {0} zone: {1}" -f $tstzn,$tstTime
" UTC Time: {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($tstTime, $tst)