TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) Metod

Definition

Konverterar en UTC (Coordinated Universal Time) till tiden i en angiven tidszon.

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

Parametrar

dateTime
DateTime

Den samordnade universella tiden (UTC).

destinationTimeZone
TimeZoneInfo

Tidszonen som ska konverteras dateTime till.

Returer

Datum och tid i måltidszonen. Dess Kind egenskap är Utc om destinationTimeZone är Utc, annars är Kinddess Unspecified egenskap .

Undantag

Egenskapen KinddateTime för är Local.

destinationTimeZone är null.

Exempel

I följande exempel konverteras UTC (Coordinated Universal Time) till Central Time.

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

Kommentarer

När du utför konverteringen ConvertTimeFromUtc tillämpar metoden eventuella justeringsregler som gäller i tidszonen destinationTimeZone .

Det exakta beteendet för den här metoden beror på värdet Kind för egenskapen för parametern dateTime , vilket visas i följande tabell.

Egenskapen DateTime.Kind Conversion
DateTimeKind.Local Kastar en ArgumentException.
DateTimeKind.Unspecified eller DateTimeKind.Utc Konverterar från Coordinated Universal Time (UTC).

Om konverteringen av dateTime resulterar i ett datum- och tidsvärde som är tidigare än DateTime.MinValue eller senare än DateTime.MaxValuereturnerar DateTime.MinValue den här metoden respektive DateTime.MaxValue.

Gäller för

Se även