How to: Instantiate a TimeZoneInfo Object

The most common way to instantiate a TimeZoneInfo object is to retrieve information about it from the registry. This topic discusses how to instantiate a TimeZoneInfo object from the local system registry.

To instantiate a TimeZoneInfo Object

  1. Declare a TimeZoneInfo object.

  2. Call the static (Shared in Visual Basic) TimeZoneInfo.FindSystemTimeZoneById method.

  3. Handle any exceptions thrown by the method, particularly the TimeZoneNotFoundException that is thrown if the time zone is not defined in the registry.

The following code retrieves a TimeZoneInfo object that represents the Eastern Standard Time zone and displays the Eastern Standard time that corresponds to the local time.

DateTime timeNow = DateTime.Now;
   TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
   DateTime easternTimeNow = TimeZoneInfo.ConvertTime(timeNow, TimeZoneInfo.Local, 
   Console.WriteLine("{0} {1} corresponds to {2} {3}.",
                     TimeZoneInfo.Local.IsDaylightSavingTime(timeNow) ?
                               TimeZoneInfo.Local.DaylightName : 
                     easternZone.IsDaylightSavingTime(easternTimeNow) ?
                                 easternZone.DaylightName : 
// Handle exception 
// As an alternative to simply displaying an error message, an alternate Eastern 
// Standard Time TimeZoneInfo object could be instantiated here either by restoring 
// it from a serialized string or by providing the necessary data to the 
// CreateCustomTimeZone method. 
catch (TimeZoneNotFoundException)
   Console.WriteLine("The Eastern Standard Time Zone cannot be found on the local system.");
catch (InvalidTimeZoneException)
   Console.WriteLine("The Eastern Standard Time Zone contains invalid or missing data.");
catch (SecurityException)
   Console.WriteLine("The application lacks permission to read time zone information from the registry.");
catch (OutOfMemoryException)
   Console.WriteLine("Not enough memory is available to load information on the Eastern Standard Time zone.");
// If we weren't passing FindSystemTimeZoneById a literal string, we also  
// would handle an ArgumentNullException.

The TimeZoneInfo.FindSystemTimeZoneById method's single parameter is the identifier of the time zone that you want to retrieve, which corresponds to the object's TimeZoneInfo.Id property. The time zone identifier is a key field that uniquely identifies the time zone. While most keys are relatively short, the time zone identifier is comparatively long. In most cases, its value corresponds to the StandardName property of a TimeZoneInfo object, which is used to provide the name of the time zone's standard time. However, there are exceptions. The best way to make sure that you supply a valid identifier is to enumerate the time zones available on your system and note the identifiers of the time zones present on them. For an illustration, see How to: Enumerate Time Zones Present on a Computer. The Finding the Time Zones Defined on a Local System topic also contains a list of selected time zone identifiers.

If the time zone is found, the method returns its TimeZoneInfo object. If the time zone is not found, the method throws a TimeZoneNotFoundException. If the time zone is found but its data is corrupted or incomplete, the method throws an InvalidTimeZoneException.

If your application relies on a time zone that must be present, you should first call the FindSystemTimeZoneById method to retrieve the time zone information from the registry. If the method call fails, your exception handler should then either create a new instance of the time zone or re-create it by deserializing a serialized TimeZoneInfo object. See How to: Restore Time Zones from an Embedded Resource for an example.

This example requires:

  • That a reference to System.Core.dll be added to the project.

  • That the System namespace be imported with the using statement (required in C# code).

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft