TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Converte uma Hora Universal Coordenada (UTC) para a hora num fuso horário especificado.
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
Parâmetros
- dateTime
- DateTime
O Tempo Universal Coordenado (UTC).
- destinationTimeZone
- TimeZoneInfo
O fuso horário para converter dateTime .
Devoluções
A data e hora no fuso horário do destino. A sua Kind propriedade é Utc se destinationTimeZone for Utc; caso contrário, a sua Kind propriedade é Unspecified.
Exceções
destinationTimeZone é null.
Exemplos
O exemplo seguinte converte o Tempo Universal Coordenado (UTC) para o Horário Central.
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
Observações
Ao realizar a conversão, o ConvertTimeFromUtc método aplica quaisquer regras de ajuste em vigor no destinationTimeZone fuso horário.
O comportamento preciso deste método depende do valor da Kind propriedade do dateTime parâmetro, como mostra a tabela seguinte.
| DateTime.Kind propriedade | Conversion |
|---|---|
| DateTimeKind.Local | Lança um ArgumentException. |
| DateTimeKind.Unspecified ou DateTimeKind.Utc | Converte do Tempo Universal Coordenado (UTC). |
Se a conversão de dateTime resultar num valor de data e hora anteriores DateTime.MinValue ou posteriores a DateTime.MaxValue, este método devolve DateTime.MinValue ou DateTime.MaxValue, respetivamente.