如何:访问预定义 UTC 和本地时区对象

TimeZoneInfo 类提供 UtcLocal 两种属性,允许代码访问预定义时区对象。 本主题介绍如何访问这些属性返回的 TimeZoneInfo 对象。

访问协调世界时 (UTC) TimeZoneInfo 对象

  1. 使用 static(在 Visual Basic 中使用 SharedTimeZoneInfo.Utc 属性访问协调世界时。

  2. 不要将该属性返回的 TimeZoneInfo 对象分配给对象变量,而是继续通过 TimeZoneInfo.Utc 属性访问协调世界时。

访问本地时区

  1. 使用 static(在 Visual Basic 中使用 SharedTimeZoneInfo.Local 属性访问本地系统时区。

  2. 不要将该属性返回的 TimeZoneInfo 对象分配给对象变量,而是继续通过 TimeZoneInfo.Local 属性访问本地时区。

示例

下面的代码使用 TimeZoneInfo.LocalTimeZoneInfo.Utc 属性转换美国和加拿大东部标准时区的时间,并将时区名称显示到控制台。

// 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

应始终通过 TimeZoneInfo.Local 属性来访问本地时区,而不是将本地时区分配给 TimeZoneInfo 对象变量。 同样,应始终通过 TimeZoneInfo.Utc 属性来访问协调世界时,而不是将 UTC 时区分配给 TimeZoneInfo 对象变量。 这可防止 TimeZoneInfo 对象变量因调用 TimeZoneInfo.ClearCachedData 方法而失效。

编译代码

此示例需要:

  • 使用 using 语句导入的 System 命名空间(在 C# 代码中是必需的)。

另请参阅