TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) Método
Definição
Converte um UTC (Tempo Universal Coordenado) no horário em um fuso horário especificado.Converts a Coordinated Universal Time (UTC) to the time in a specified time zone.
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
UTC (Tempo Universal Coordenado).The Coordinated Universal Time (UTC).
- destinationTimeZone
- TimeZoneInfo
O fuso horário no qual dateTime
deve ser convertido.The time zone to convert dateTime
to.
Retornos
A data e hora no fuso horário de destino.The date and time in the destination time zone. A propriedade Kind será Utc se destinationTimeZone
for Utc; caso contrário, a propriedade Kind será Unspecified.Its Kind property is Utc if destinationTimeZone
is Utc; otherwise, its Kind property is Unspecified.
Exceções
destinationTimeZone
é null
.destinationTimeZone
is null
.
Exemplos
O exemplo a seguir converte o tempo universal coordenado (UTC) para o horário central.The following example converts Coordinated Universal Time (UTC) to 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.");
}
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
Comentários
Ao executar a conversão, o ConvertTimeFromUtc método aplica quaisquer regras de ajuste em vigor no destinationTimeZone
fuso horário.When performing the conversion, the ConvertTimeFromUtc method applies any adjustment rules in effect in the destinationTimeZone
time zone.
O comportamento preciso desse método depende do valor da Kind Propriedade do dateTime
parâmetro, como mostra a tabela a seguir.The precise behavior of this method depends on the value of the Kind property of the dateTime
parameter, as the following table shows.
Propriedade DateTime.KindDateTime.Kind property | ConversãoConversion |
---|---|
DateTimeKind.Local | Lança um ArgumentException.Throws an ArgumentException. |
DateTimeKind.Unspecified ou DateTimeKind.UtcDateTimeKind.Unspecified or DateTimeKind.Utc | Converte do UTC (tempo Universal Coordenado).Converts from Coordinated Universal Time (UTC). |
Caso a conversão de dateTime
resulte em uma data e um valor de hora anterior a DateTime.MinValue ou posterior a DateTime.MaxValue, este método retorna DateTime.MinValue ou DateTime.MaxValue, respectivamente.If the conversion of dateTime
results in a date and time value that is earlier than DateTime.MinValue or later than DateTime.MaxValue, this method returns DateTime.MinValue or DateTime.MaxValue, respectively.