Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 3.7 KB

instantiate-time-zone-info.md

File metadata and controls

37 lines (27 loc) · 3.7 KB
title description ms.date dev_langs helpviewer_keywords ms.topic
How to: Obtain a TimeZoneInfo object
Learn how to obtain a TimeZoneInfo object
03/06/2024
csharp
vb
instantiating time zone objects
time zone objects [.NET], instantiation
how-to

How to: Obtain a TimeZoneInfo object

The most common way to obtain a xref:System.TimeZoneInfo object is to retrieve information about it from the registry. To obtain the object, call the static (Shared in Visual Basic) xref:System.TimeZoneInfo.FindSystemTimeZoneById%2A?displayProperty=nameWithType method, which looks in the registry. Handle any exceptions thrown by the method, particularly the xref:System.TimeZoneNotFoundException that's thrown if the time zone isn't defined in the registry.

Note

Starting in .NET 8, xref:System.TimeZoneInfo.FindSystemTimeZoneById%2A?displayProperty=nameWithType returns a cached xref:System.TimeZoneInfo object instead of instantiating a new object. For more information, see FindSystemTimeZoneById doesn't return new object.

Example

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

[!code-csharpSystem.TimeZone2.Concepts#5] [!code-vbSystem.TimeZone2.Concepts#5]

The xref:System.TimeZoneInfo.FindSystemTimeZoneById%2A?displayProperty=nameWithType method's single parameter is the identifier of the time zone that you want to retrieve, which corresponds to the object's xref:System.TimeZoneInfo.Id%2A?displayProperty=nameWithType 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 xref:System.TimeZoneInfo.StandardName property of a xref:System.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 article also contains a list of selected time-zone identifiers.

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

If your application relies on a time zone that must be present, you should first call the xref:System.TimeZoneInfo.FindSystemTimeZoneById%2A 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 xref:System.TimeZoneInfo object. See How to: Restore time zones from an embedded resource for an example.

See also