TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) 方法

定义

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

public:
 static DateTime ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo ^ destinationTimeZone);
public static DateTime ConvertTimeFromUtc (DateTime dateTime, TimeZoneInfo destinationTimeZone);
static member ConvertTimeFromUtc : DateTime * TimeZoneInfo -> DateTime
Public Shared Function ConvertTimeFromUtc (dateTime As DateTime, destinationTimeZone As TimeZoneInfo) As DateTime

参数

dateTime
DateTime

协调世界时 (UTC)。

destinationTimeZone
TimeZoneInfo

要将 dateTime 转换到的时区。

返回

目标时区的日期和时间。 如果 destinationTimeZoneUtc,则其 Kind 属性为 Utc;否则其 Kind 属性为 Unspecified

例外

dateTimeKind 属性为 Local

destinationTimeZonenull

示例

以下示例将协调世界时 (UTC) 转换为中部时间。

DateTime timeUtc = DateTime.UtcNow;
try
{
   TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
   DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
   Console.WriteLine("The date and time are {0} {1}.", 
                     cstTime, 
                     cstZone.IsDaylightSavingTime(cstTime) ?
                             cstZone.DaylightName : cstZone.StandardName);
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("The registry does not define the Central Standard Time zone.");
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Registry data on the Central Standard Time zone has been corrupted.");
}
let timeUtc = DateTime.UtcNow
try
    let cstZone = TimeZoneInfo.FindSystemTimeZoneById "Central Standard Time"
    let cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone)
    printfn $"The date and time are {cstTime} {if cstZone.IsDaylightSavingTime cstTime then cstZone.DaylightName else cstZone.StandardName}."
with
| :? TimeZoneNotFoundException ->
    printfn "The registry does not define the Central Standard Time zone."
| :? InvalidTimeZoneException ->
    printfn "Registry data on the Central Standard Time zone has been corrupted."
Dim timeUtc As Date = Date.UtcNow
Try
   Dim cstZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")
   Dim cstTime As Date = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone)
   Console.WriteLine("The date and time are {0} {1}.", _
                     cstTime, _
                     IIf(cstZone.IsDaylightSavingTime(cstTime), _
                         cstZone.DaylightName, cstZone.StandardName))
Catch e As TimeZoneNotFoundException
   Console.WriteLine("The registry does not define the Central Standard Time zone.")
Catch e As InvalidTimeZoneException
   Console.WriteLine("Registry data on the Central Standard Time zone has been corrupted.")
End Try

注解

执行转换时, ConvertTimeFromUtc 方法将应用时区中 destinationTimeZone 生效的任何调整规则。

此方法的精确行为取决于 参数的 KinddateTime 属性值,如下表所示。

DateTime.Kind 属性 转换
DateTimeKind.Local 引发 ArgumentException
DateTimeKind.UnspecifiedDateTimeKind.Utc 从协调世界时 (UTC) 转换。

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

适用于

另请参阅