System.TimeZoneInfo.ConvertTime 方法 (DateTime, TimeZoneInfo)

方法描述

将时间转换为特定时区的时间。

语法定义(C# System.TimeZoneInfo.ConvertTime 方法 (DateTime, TimeZoneInfo) 的用法)

public static DateTime ConvertTime(
	DateTime dateTime,
	TimeZoneInfo destinationTimeZone
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
dateTime System-DateTime 要转换的日期和时间。
destinationTimeZone System-TimeZoneInfo 要将 dateTime 转换到的时区。
返回值 System.DateTime 目标时区的日期和时间。

提示和注释

执行转换时,ConvertTime(DateTimeOffset, TimeZoneInfo) 方法将有效应用 destinationTimeZone 时区中的任何调整规则。

此 ConvertTime(DateTime, TimeZoneInfo) 方法重载通过 dateTime 参数的 Kind 属性值确定源时区,如下表所示。

Kind 属性值

源时区

方法行为

DateTimeKind.Local

Local

将本地时间转换为 destinationTimeZone 中的时间。

DateTimeKind.Utc

Utc

将协调世界时 (UTC) 转换为 destinationTimeZone 中的时间。

DateTimeKind.Unspecified

假定为 Local。

将本地时间转换为 destinationTimeZone 中的时间。

如下表所示设置返回的 DateTime 值的 Kind 属性。

Condition

返回的 Kind 属性值

destinationTimeZone 参数为 TimeZoneInfo.Utc。

DateTimeKind.Utc

dateTime 的 Kind 属性为 DateTimeKind.Local,并且 destinationTimeZone 为 DateTimeKind.Local。

DateTimeKind.Local

所有其他日期和时间值以及目标时区。

DateTimeKind.Unspecified

如果 dateTime 参数值为不明确的本地时间,则将该时间解释为标准时间。 如果 dateTime 参数为无效的本地时间,则此方法会引发 ArgumentException。

如果 dateTime 的转换结果为早于 DateTime.MinValue 或晚于 DateTime.MaxValue 的日期和时间值,则此方法分别返回 DateTime.MinValue 或 DateTime.MaxValue。

您还可以通过调用 ConvertTimeFromUtc 和 ConvertTimeToUtc 方法转换成 UTC 或者从 UTC 进行转换。

对调用者的说明

在 Windows XP 系统中,如果把 dateTime 视为本地时间或者如果 destinationTimeZone 是 TimeZoneInfo.Local,那么该方法执行转换时只会识别当前的调整规则。 因此,该方法在当前调整规则生效之前可能无法准确转换时间段。 有关更多信息,请参见 Local 属性中的“调用方说明”一节。

System.TimeZoneInfo.ConvertTime 方法 (DateTime, TimeZoneInfo)例子

在 2010 年 11 月 7 日。

using System;

public class Example
{
   public static void Main()
   {
      // Define times to be converted.
      DateTime[] times = { new DateTime(2010, 1, 1, 0, 1, 0), 
                           new DateTime(2010, 1, 1, 0, 1, 0, DateTimeKind.Utc), 
                           new DateTime(2010, 1, 1, 0, 1, 0, DateTimeKind.Local),                            
                           new DateTime(2010, 11, 6, 23, 30, 0),
                           new DateTime(2010, 11, 7, 2, 30, 0) };

      // Retrieve the time zone for Eastern Standard Time (U.S. and Canada).
      TimeZoneInfo est; 
      try {
         est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
      }
      catch (TimeZoneNotFoundException) {
         Console.WriteLine("Unable to retrieve the Eastern Standard time zone.");
         return;
      }
      catch (InvalidTimeZoneException) {
         Console.WriteLine("Unable to retrieve the Eastern Standard time zone.");
         return;
      }   

      // Display the current time zone name.
      Console.WriteLine("Local time zone: {0}\n", TimeZoneInfo.Local.DisplayName);

      // Convert each time in the array.
      foreach (DateTime timeToConvert in times)
      {
         DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est);
         Console.WriteLine("Converted {0} {1} to {2}.", timeToConvert, 
                           timeToConvert.Kind, targetTime);
      }                        
   }
}
// The example displays the following output:
//    Local time zone: (GMT-08:00) Pacific Time (US & Canada)
//    
//    Converted 1/1/2010 12:01:00 AM Unspecified to 1/1/2010 3:01:00 AM.
//    Converted 1/1/2010 12:01:00 AM Utc to 12/31/2009 7:01:00 PM.
//    Converted 1/1/2010 12:01:00 AM Local to 1/1/2010 3:01:00 AM.
//    Converted 11/6/2010 11:30:00 PM Unspecified to 11/7/2010 1:30:00 AM.
//    Converted 11/7/2010 2:30:00 AM Unspecified to 11/7/2010 5:30:00 AM.

异常

异常 异常描述
ArgumentException dateTime 参数值表示无效时间。
ArgumentNullException destinationTimeZone 参数的值为 null。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。