語言

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 成的時區。

傳回

目的地時區的日期和時間。 Kind若Utc為 則destinationTimeZone其性質為 Utc ;否則,其Kind性質為 Unspecified。

例外狀況

Kind 的 dateTime 屬性是 Local。

destinationTimeZone 是 null。

範例

以下範例將協調世界時(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 屬性 Conversion
DateTimeKind.Local 投擲一個 ArgumentException.
DateTimeKind.Unspecified 或 DateTimeKind.Utc 從協調世界時(UTC)轉換過來。

若 dateTime 轉換所得的日期與時間值早 DateTime.MinValue 於或晚 DateTime.MaxValue於 ,則該方法分別回傳 DateTime.MinValue 或 DateTime.MaxValue。

適用於

另請參閱