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

TimeZoneInfo 类提供了 UtcLocal 两个属性,这两个属性允许您通过代码访问预定义的时区对象。 本主题讨论如何访问由这两个属性返回的 TimeZoneInfo 对象。

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

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

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

访问本地时区

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

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

示例

下面的代码使用 TimeZoneInfo.LocalTimeZoneInfo.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
// 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 {0} zone cannot be found in the registry.", 
                     timeZoneName);
}
catch (InvalidTimeZoneException)
{
   Console.WriteLine("The registry contains invalid data for the {0} zone.", 
                     timeZoneName);
}

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

编译代码

此示例需要:

  • 在项目中添加一个对 System.Core.dll 的引用。

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

请参见

任务

如何:实例化 TimeZoneInfo 对象

概念

查找在本地系统上定义的时区

其他资源

日期、时间和时区