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 转换到的时区。

返回

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 的任何调整规则。

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

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

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

适用于

另请参阅