Export (0) Print
Expand All

How to: Access the Predefined UTC and Local Time Zone Objects

The TimeZoneInfo class provides two properties, Utc and Local, that give your code access to predefined time zone objects. This topic discusses how to access the TimeZoneInfo objects returned by those properties.

To access the Coordinated Universal Time (UTC) TimeZoneInfo object

  1. Use the static (Shared in Visual Basic) TimeZoneInfo.Utc property to access Coordinated Universal Time.

  2. Rather than assigning the TimeZoneInfo object returned by the property to an object variable, continue to access Coordinated Universal Time through the TimeZoneInfo.Utc property.

To access the local time zone

  1. Use the static (Shared in Visual Basic) TimeZoneInfo.Local property to access the local system time zone.

  2. Rather than assigning the TimeZoneInfo object returned by the property to an object variable, continue to access the local time zone through the TimeZoneInfo.Local property.

The following code uses the TimeZoneInfo.Local and TimeZoneInfo.Utc properties to convert a time from the U.S. and Canadian Eastern Standard time zone, as well as to display the time zone name to the console.

' Create Eastern Standard Time value and TimeZoneInfo object       
Dim estTime As Date = #01/01/2007 00:00:00#
Dim timeZoneName As String = "Eastern Standard Time" 
Try 
   Dim est As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName)

   ' Convert EST to local time 
   Dim localTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local)
   Console.WriteLine("At {0} {1}, the local time is {2} {3}.", _
           estTime, _
           est, _
           localTime, _
           IIf(TimeZoneInfo.Local.IsDaylightSavingTime(localTime), _
               TimeZoneInfo.Local.DaylightName, _
               TimeZoneInfo.Local.StandardName))

   ' Convert EST to UTC 
   Dim utcTime As Date = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc)
   Console.WriteLine("At {0} {1}, the time is {2} {3}.", _
           estTime, _
           est, _
           utcTime, _
           TimeZoneInfo.Utc.StandardName)
Catch e As TimeZoneNotFoundException
   Console.WriteLine("The {0} zone cannot be found in the registry.", _
                     timeZoneName)
Catch e As InvalidTimeZoneException
   Console.WriteLine("The registry contains invalid data for the {0} zone.", _
                     timeZoneName)
End Try

You should always access the local time zone through the TimeZoneInfo.Local property rather than assigning the local time zone to a TimeZoneInfo object variable. Similarly, you should always access Coordinated Universal Time through the TimeZoneInfo.Utc property rather than assigning the UTC zone to a TimeZoneInfo object variable. This prevents the TimeZoneInfo object variable from being invalidated by a call to the TimeZoneInfo.ClearCachedData method.

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).

Show:
© 2014 Microsoft