Bewerken

Share via


TimeZoneInfo.FindSystemTimeZoneById(String) Method

Definition

Returns a TimeZoneInfo object based on its identifier.

public:
 static TimeZoneInfo ^ FindSystemTimeZoneById(System::String ^ id);
public static TimeZoneInfo FindSystemTimeZoneById (string id);
static member FindSystemTimeZoneById : string -> TimeZoneInfo
Public Shared Function FindSystemTimeZoneById (id As String) As TimeZoneInfo

Parameters

id
String

The time zone identifier, which corresponds to the Id property.

Returns

An object whose identifier is the value of the id parameter.

Exceptions

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

The id parameter is null.

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

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

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

Examples

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.

using System;

public class Example
{
   public static void Main()
   {
      // 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}", tst.IsDaylightSavingTime(tstTime) ?
                        tst.DaylightName : tst.StandardName, tstTime);
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));
   }
}
// The example displays output like the following when run on a system in the
// U.S. Pacific Standard Time zone:
//       Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
//       Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
open System

// Get time in local time zone 
let thisTime = DateTime.Now
printfn $"Time in {if TimeZoneInfo.Local.IsDaylightSavingTime thisTime then TimeZoneInfo.Local.DaylightName else TimeZoneInfo.Local.StandardName} zone: {thisTime}"
printfn $"   UTC Time: {TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local)}"
// Get Tokyo Standard Time zone
let tst = TimeZoneInfo.FindSystemTimeZoneById "Tokyo Standard Time"
let tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst)      
printfn $"Time in {if tst.IsDaylightSavingTime tstTime then tst.DaylightName else tst.StandardName} zone: {tstTime}"
printfn $"   UTC Time: {TimeZoneInfo.ConvertTimeToUtc(tstTime, tst)}"
// The example displays output like the following when run on a system in the
// U.S. Pacific Standard Time zone:
//       Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
//       Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
Module Example
   Public Sub Main()
      ' Get time in local time zone 
      Dim thisTime As Date = Date.Now
      Console.WriteLine("Time in {0} zone: {1}", IIf(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
      Dim tst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")
      Dim tstTime As Date = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst)      
      Console.WriteLine("Time in {0} zone: {1}", IIf(tst.IsDaylightSavingTime(tstTime), 
                        tst.DaylightName, tst.StandardName), tstTime)
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst))
   End Sub
End Module
' The example displays output like the following when run on a system in the U.S.
' Pacific Standard Time zone:
'    Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
'       UTC Time: 12/6/2013 6:57:51 PM
'    Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
'       UTC Time: 12/6/2013 6:57:51 PM

Remarks

The id parameter must correspond exactly to the time zone's identifier 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.

On Windows systems, 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. On Linux and macOS, it uses time zone information available in the ICU Library. If the registry or the library does not have the information for the time zone you desire, 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 system-defined time 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.

In .NET 7 and earlier versions, this method returns a new TimeZoneInfo instance for each method call. This might impact performance in applications that call the FindSystemTimeZoneById method repeatedly with the same identifier. (In .NET 8 and later versions, this method always returns a cached TimeZoneInfo instance.)

Applies to

See also