Edit

Share via


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.

Example

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
DateTime estTime = new DateTime(2007, 1, 1, 00, 00, 00);
string timeZoneName = "Eastern Standard Time";
try
{
    TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);

    // Convert EST to local time
    DateTime localTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Local);
    Console.WriteLine("At {0} {1}, the local time is {2} {3}.",
            estTime,
            est,
            localTime,
            TimeZoneInfo.Local.IsDaylightSavingTime(localTime) ?
                      TimeZoneInfo.Local.DaylightName :
                      TimeZoneInfo.Local.StandardName);

    // Convert EST to UTC
    DateTime utcTime = TimeZoneInfo.ConvertTime(estTime, est, TimeZoneInfo.Utc);
    Console.WriteLine("At {0} {1}, the time is {2} {3}.",
            estTime,
            est,
            utcTime,
            TimeZoneInfo.Utc.StandardName);
}
catch (TimeZoneNotFoundException)
{
    Console.WriteLine("The {timeZoneName} zone cannot be found in the registry.");
}
catch (InvalidTimeZoneException)
{
    Console.WriteLine("The registry contains invalid data for the {timeZoneName} zone.");
}

// The example produces the following output to the console:
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the local time is 1/1/2007 12:00:00 AM Eastern Standard Time.
//    At 1/1/2007 12:00:00 AM (UTC-05:00) Eastern Time (US & Canada), the time is 1/1/2007 5:00:00 AM UTC.

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

See also