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 生效的任何調整規則。

此方法的精確行為取決於 Kind 參數的 dateTime 屬性值,如下表所示。

DateTime.Kind 屬性 轉換
DateTimeKind.Local 擲回 ArgumentException
DateTimeKind.UnspecifiedDateTimeKind.Utc 從國際標準時間 (UTC) 轉換。

如果轉換 dateTime 結果的日期和時間值早于 DateTime.MinValue 或 晚 DateTime.MaxValue ,則此方法會分別傳回 DateTime.MinValueDateTime.MaxValue

適用於

另請參閱