System.TimeZoneInfo.ConvertTime 方法 (DateTimeOffset, TimeZoneInfo)
方法描述
将时间转换为特定时区的时间。
语法定义(C# System.TimeZoneInfo.ConvertTime 方法 (DateTimeOffset, TimeZoneInfo) 的用法)
public static DateTimeOffset ConvertTime( DateTimeOffset dateTimeOffset, TimeZoneInfo destinationTimeZone )
参数/返回值
| 参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 |
|---|---|---|
| dateTimeOffset | System-DateTimeOffset | 要转换的日期和时间。 |
| destinationTimeZone | System-TimeZoneInfo | 要将 dateTime 转换到的时区。 |
| 返回值 | System.DateTimeOffset | 目标时区的日期和时间。 |
提示和注释
执行转换时,ConvertTime(DateTimeOffset, TimeZoneInfo) 方法将有效应用 destinationTimeZone 时区中的任何调整规则。
此重载与 ConvertTime 方法的其他重载的不同之处在于,它接受 DateTimeOffset 值作为其第一个参数。 这样就将日期和时间识别为与协调世界时 (UTC) 的偏移量,而不是特定时区中的日期和时间。 因此,dateTimeOffset 参数不能表示不明确的时间或无效时间。
在将 dateTimeOffset 值转换为目标时区时间的过程中,此方法会考虑目标时区中所有有效的调整规则。
如果 dateTimeOffset 的转换结果为早于 DateTimeOffset.MinValue 或晚于 DateTimeOffset.MaxValue 的日期和时间值,则此方法分别返回 DateTimeOffset.MinValue 或 DateTimeOffset.MaxValue。
对调用者的说明
在 Windows XP 系统中,如果把 dateTimeOffset 视为本地时间或者如果 destinationTimeZone 是 TimeZoneInfo.Local,那么该方法只将当前调整规则应用于时区转换。 因此,该方法在当前调整规则生效之前可能无法准确转换时间段。 有关更多信息,请参见 Local 属性中的“调用方说明”一节。
System.TimeZoneInfo.ConvertTime 方法 (DateTimeOffset, TimeZoneInfo)例子
在 2010 年 11 月 7 日。
using System;
public class Example
{
public static void Main()
{
// Define times to be converted.
DateTime time1 = new DateTime(2010, 1, 1, 12, 1, 0);
DateTime time2 = new DateTime(2010, 11, 6, 23, 30, 0);
DateTimeOffset[] times = { new DateTimeOffset(time1, TimeZoneInfo.Local.GetUtcOffset(time1)),
new DateTimeOffset(time1, TimeSpan.Zero),
new DateTimeOffset(time2, TimeZoneInfo.Local.GetUtcOffset(time2)),
new DateTimeOffset(time2.AddHours(3), TimeZoneInfo.Local.GetUtcOffset(time2.AddHours(3))) };
// 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 (DateTimeOffset timeToConvert in times)
{
DateTimeOffset targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est);
Console.WriteLine("Converted {0} to {1}.", timeToConvert, 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 -08:00 to 1/1/2010 3:01:00 AM -05:00.
// Converted 1/1/2010 12:01:00 AM +00:00 to 12/31/2009 7:01:00 PM -05:00.
// Converted 11/6/2010 11:30:00 PM -07:00 to 11/7/2010 1:30:00 AM -05:00.
// Converted 11/7/2010 2:30:00 AM -08:00 to 11/7/2010 5:30:00 AM -05:00.
异常
| 异常 | 异常描述 |
|---|---|
| ArgumentNullException | destinationTimeZone 参数的值为 null。 |
版本信息
.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 系统要求。