TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) Metoda

Definicja

Konwertuje uniwersalny czas koordynowany (UTC) na godzinę w określonej strefie czasowej.

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

Parametry

dateTime
DateTime

Uniwersalny czas koordynowany (UTC).

destinationTimeZone
TimeZoneInfo

Strefa czasowa do przekonwertowania dateTime na.

Zwraca

Data i godzina w docelowej strefie czasowej. Jego Kind właściwość to , jeśli destinationTimeZone jest UtcUtc; w przeciwnym razie jego Kind właściwość to Unspecified.

Wyjątki

Właściwość Kind to dateTimeLocal.

destinationTimeZone to null.

Przykłady

Poniższy przykład konwertuje uniwersalny czas koordynowany (UTC) na czas centralny.

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

Uwagi

Podczas przeprowadzania konwersji ConvertTimeFromUtc metoda stosuje wszelkie reguły korekty obowiązujące w strefie czasowej destinationTimeZone .

Dokładne zachowanie tej metody zależy od wartości Kind właściwości parametru dateTime , jak pokazano w poniższej tabeli.

Właściwość DateTime.Kind Konwersja
DateTimeKind.Local Zgłasza element ArgumentException.
DateTimeKind.Unspecified lub DateTimeKind.Utc Konwertuje z uniwersalnego czasu koordynowanego (UTC).

Jeśli konwersja dateTime wyników ma wartość daty i godziny, która jest wcześniejsza niż lub nowsza niż DateTime.MaxValueDateTime.MinValue , ta metoda zwraca DateTime.MinValue wartość lub DateTime.MaxValue, odpowiednio.

Dotyczy

Zobacz też