TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) Metodo

Definizione

Converte l'ora UTC (Coordinated Universal Time) nell'ora di un fuso orario specificato.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

Parametri

dateTime
DateTime

Ora UTC (Coordinated Universal Time).The Coordinated Universal Time (UTC).

destinationTimeZone
TimeZoneInfo

Fuso orario nel quale convertire dateTime.The time zone to convert dateTime to.

Restituisce

DateTime

Data e ora nel fuso orario di destinazione.The date and time in the destination time zone. La proprietà Kind è Utc se destinationTimeZone è Utc; in caso contrario, la proprietà Kind è Unspecified.Its Kind property is Utc if destinationTimeZone is Utc; otherwise, its Kind property is Unspecified.

Eccezioni

La proprietà Kind di dateTime è Local.The Kind property of dateTime is Local.

destinationTimeZone è null.destinationTimeZone is null.

Esempio

Nell'esempio seguente viene convertito l'ora UTC (Coordinated Universal Time) nell'ora centrale.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

Commenti

Quando si esegue la conversione, il ConvertTimeFromUtc metodo applica eventuali regole di rettifica attive nel destinationTimeZone fuso orario.When performing the conversion, the ConvertTimeFromUtc method applies any adjustment rules in effect in the destinationTimeZone time zone.

Il comportamento preciso di questo metodo dipende dal valore della Kind proprietà del dateTime parametro, come illustrato nella tabella seguente.The precise behavior of this method depends on the value of the Kind property of the dateTime parameter, as the following table shows.

Proprietà DateTime.KindDateTime.Kind property ConversioneConversion
DateTimeKind.Local Genera un'eccezione ArgumentException .Throws an ArgumentException.
DateTimeKind.Unspecified o DateTimeKind.UtcDateTimeKind.Unspecified or DateTimeKind.Utc Esegue la conversione dall'ora UTC (Coordinated Universal Time).Converts from Coordinated Universal Time (UTC).

Se la conversione di dateTime restituisce un valore di data e ora precedente DateTime.MinValue o successiva a DateTime.MaxValue , questo metodo restituisce DateTime.MinValue DateTime.MaxValue rispettivamente o.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.

Si applica a