TimeZoneInfo.ConvertTimeToUtc Metodo

Definizione

Converte una data e un'ora in ora UTC (Coordinated Universal Time).Converts a date and time to Coordinated Universal Time (UTC).

Overload

ConvertTimeToUtc(DateTime, TimeZoneInfo)

Converte l'ora di un fuso orario specificato in ora UTC (Coordinated Universal Time).Converts the time in a specified time zone to Coordinated Universal Time (UTC).

ConvertTimeToUtc(DateTime)

Converte la data e l'ora specificate in ora UTC (Coordinated Universal Time).Converts the specified date and time to Coordinated Universal Time (UTC).

ConvertTimeToUtc(DateTime, TimeZoneInfo)

Converte l'ora di un fuso orario specificato in ora UTC (Coordinated Universal Time).Converts the time in a specified time zone to Coordinated Universal Time (UTC).

public:
 static DateTime ConvertTimeToUtc(DateTime dateTime, TimeZoneInfo ^ sourceTimeZone);
public static DateTime ConvertTimeToUtc (DateTime dateTime, TimeZoneInfo sourceTimeZone);
static member ConvertTimeToUtc : DateTime * TimeZoneInfo -> DateTime
Public Shared Function ConvertTimeToUtc (dateTime As DateTime, sourceTimeZone As TimeZoneInfo) As DateTime

Parametri

dateTime
DateTime

Data e ora da convertire.The date and time to convert.

sourceTimeZone
TimeZoneInfo

Fuso orario di dateTime.The time zone of dateTime.

Restituisce

DateTime

Ora UTC (Coordinated Universal Time) corrispondente al parametro dateTime.The Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. La proprietà Kind dell'oggetto DateTime è sempre impostata su Utc.The DateTime object's Kind property is always set to Utc.

Eccezioni

dateTime.KinddateTime.Kind è Utc e sourceTimeZone non è uguale a Utc.is Utc and sourceTimeZone does not equal Utc.

-oppure--or- dateTime.KinddateTime.Kind è Local e sourceTimeZone non è uguale a Local.is Local and sourceTimeZone does not equal Local.

-oppure--or- sourceTimeZone.IsInvalidDateTime(dateTime) restituisce true.sourceTimeZone.IsInvalidDateTime(dateTime) returns true.

sourceTimeZone è null.sourceTimeZone is null.

Esempio

Nell'esempio seguente viene recuperata la data corrente dal sistema locale e viene convertita nel formato UTC (Coordinated Universal Time), quindi viene convertita nell'ora solare Tokyo e infine viene convertito dall'ora solare di Tokyo all'ora UTC.The following example retrieves the current date from the local system and converts it to Coordinated Universal Time (UTC), then converts it to Tokyo Standard Time, and finally converts from Tokyo Standard Time back to UTC. Si noti che le due ore UTC sono identiche.Note that the two UTC times are identical.

using System;

public class Example
{
   public static void Main()
   {
      // Get time in local time zone 
      DateTime thisTime = DateTime.Now;
      Console.WriteLine("Time in {0} zone: {1}", TimeZoneInfo.Local.IsDaylightSavingTime(thisTime) ?
                        TimeZoneInfo.Local.DaylightName : TimeZoneInfo.Local.StandardName, thisTime);
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local));
      // Get Tokyo Standard Time zone
      TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
      DateTime tstTime = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst);      
      Console.WriteLine("Time in {0} zone: {1}", tst.IsDaylightSavingTime(tstTime) ?
                        tst.DaylightName : tst.StandardName, tstTime);
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst));
   }
}
// The example displays output like the following when run on a system in the
// U.S. Pacific Standard Time zone:
//       Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
//       Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
//          UTC Time: 12/6/2013 6:57:51 PM
Module Example
   Public Sub Main()
      ' Get time in local time zone 
      Dim thisTime As Date = Date.Now
      Console.WriteLine("Time in {0} zone: {1}", IIf(TimeZoneInfo.Local.IsDaylightSavingTime(thisTime), 
                        TimeZoneInfo.Local.DaylightName, TimeZoneInfo.Local.StandardName), thisTime)
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(thisTime, TimeZoneInfo.Local))
      ' Get Tokyo Standard Time zone
      Dim tst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time")
      Dim tstTime As Date = TimeZoneInfo.ConvertTime(thisTime, TimeZoneInfo.Local, tst)      
      Console.WriteLine("Time in {0} zone: {1}", IIf(tst.IsDaylightSavingTime(tstTime), 
                        tst.DaylightName, tst.StandardName), tstTime)
      Console.WriteLine("   UTC Time: {0}", TimeZoneInfo.ConvertTimeToUtc(tstTime, tst))
   End Sub
End Module
' The example displays output like the following when run on a system in the U.S.
' Pacific Standard Time zone:
'    Time in Pacific Standard Time zone: 12/6/2013 10:57:51 AM
'       UTC Time: 12/6/2013 6:57:51 PM
'    Time in Tokyo Standard Time zone: 12/7/2013 3:57:51 AM
'       UTC Time: 12/6/2013 6:57:51 PM

Commenti

Se la Kind proprietà del dateTime parametro è uguale DateTimeKind.Utc a e il sourceTimeZone parametro è uguale a TimeZoneInfo.Utc , questo metodo restituisce dateTime senza eseguire alcuna conversione.If the Kind property of the dateTime parameter equals DateTimeKind.Utc and the sourceTimeZone parameter equals TimeZoneInfo.Utc, this method returns dateTime without performing any conversion.

Se dateTime corrisponde a un'ora ambigua, questo metodo presuppone che si tratta dell'ora solare del fuso orario di origine.If dateTime corresponds to an ambiguous time, this method assumes that it is the standard time of the source time zone. Se dateTime corrisponde a un'ora non valida, questo metodo genera un'eccezione ArgumentException .If dateTime corresponds to an invalid time, this method throws an ArgumentException.

Se l'equivalente dell'ora UTC (Coordinated Universal Time) di dateTime è precedente a DateTime.MinValue o successivo DateTime.MaxValue , questo metodo restituisce MinValue MaxValue rispettivamente o.If the Coordinated Universal Time (UTC) equivalent of dateTime is earlier than DateTime.MinValue or later that DateTime.MaxValue, this method returns MinValue or MaxValue, respectively.

Si applica a

ConvertTimeToUtc(DateTime)

Converte la data e l'ora specificate in ora UTC (Coordinated Universal Time).Converts the specified date and time to Coordinated Universal Time (UTC).

public:
 static DateTime ConvertTimeToUtc(DateTime dateTime);
public static DateTime ConvertTimeToUtc (DateTime dateTime);
static member ConvertTimeToUtc : DateTime -> DateTime
Public Shared Function ConvertTimeToUtc (dateTime As DateTime) As DateTime

Parametri

dateTime
DateTime

Data e ora da convertire.The date and time to convert.

Restituisce

DateTime

Ora UTC (Coordinated Universal Time) corrispondente al parametro dateTime.The Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. La proprietà Kind del valore DateTime è sempre impostata su Utc.The DateTime value's Kind property is always set to Utc.

Eccezioni

TimeZoneInfo.Local.IsInvalidDateTime( dateTime ) restituisce true.TimeZoneInfo.Local.IsInvalidDateTime( dateTime ) returns true.

Esempio

Nell'esempio seguente viene illustrata la conversione dei valori di ora la cui Kind proprietà è DateTimeKind.Utc , DateTimeKind.Local e DateTimeKind.Unspecified , rispettivamente.The following example illustrates the conversion of time values whose Kind property is DateTimeKind.Utc, DateTimeKind.Local, and DateTimeKind.Unspecified, respectively. Viene inoltre illustrata la conversione di orari ambigui e non validi.It also illustrates the conversion of ambiguous and invalid times.

DateTime datNowLocal = DateTime.Now;
Console.WriteLine("Converting {0}, Kind {1}:", datNowLocal, datNowLocal.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowLocal), TimeZoneInfo.ConvertTimeToUtc(datNowLocal).Kind);
Console.WriteLine();

DateTime datNowUtc = DateTime.UtcNow;
Console.WriteLine("Converting {0}, Kind {1}", datNowUtc, datNowUtc.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowUtc), TimeZoneInfo.ConvertTimeToUtc(datNowUtc).Kind);
Console.WriteLine();

DateTime datNow = new DateTime(2007, 10, 26, 13, 32, 00);
Console.WriteLine("Converting {0}, Kind {1}", datNow, datNow.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNow), TimeZoneInfo.ConvertTimeToUtc(datNow).Kind);
Console.WriteLine();

DateTime datAmbiguous = new DateTime(2007, 11, 4, 1, 30, 00);    
Console.WriteLine("Converting {0}, Kind {1}, Ambiguous {2}", datAmbiguous, datAmbiguous.Kind, TimeZoneInfo.Local.IsAmbiguousTime(datAmbiguous));
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datAmbiguous), TimeZoneInfo.ConvertTimeToUtc(datAmbiguous).Kind);
Console.WriteLine();

DateTime datInvalid = new DateTime(2007, 3, 11, 02, 30, 00);    
Console.WriteLine("Converting {0}, Kind {1}, Invalid {2}", datInvalid, datInvalid.Kind, TimeZoneInfo.Local.IsInvalidTime(datInvalid));
try
{
   Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datInvalid), TimeZoneInfo.ConvertTimeToUtc(datInvalid).Kind);
}
catch (ArgumentException e)
{   
   Console.WriteLine("   {0}: Cannot convert {1} to UTC.", e.GetType().Name, datInvalid);
}
Console.WriteLine();

DateTime datNearMax = new DateTime(9999, 12, 31, 22, 00, 00);
Console.WriteLine("Converting {0}, Kind {1}", datNearMax, datNearMax.Kind);
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNearMax), TimeZoneInfo.ConvertTimeToUtc(datNearMax).Kind);
Console.WriteLine();
//
// This example produces the following output if the local time zone 
// is Pacific Standard Time:
//
//    Converting 8/31/2007 2:26:28 PM, Kind Local:
//       ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc
//    
//    Converting 8/31/2007 9:26:28 PM, Kind Utc
//       ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc
//    
//    Converting 10/26/2007 1:32:00 PM, Kind Unspecified
//       ConvertTimeToUtc: 10/26/2007 8:32:00 PM, Kind Utc
//    
//    Converting 11/4/2007 1:30:00 AM, Kind Unspecified, Ambiguous True
//       ConvertTimeToUtc: 11/4/2007 9:30:00 AM, Kind Utc
//    
//    Converting 3/11/2007 2:30:00 AM, Kind Unspecified, Invalid True
//       ArgumentException: Cannot convert 3/11/2007 2:30:00 AM to UTC.
//    
//    Converting 12/31/9999 10:00:00 PM, Kind Unspecified
//       ConvertTimeToUtc: 12/31/9999 11:59:59 PM, Kind Utc
// 
Dim datNowLocal As Date = Date.Now
Console.WriteLine("Converting {0}, Kind {1}:", datNowLocal, datNowLocal.Kind)
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowLocal), TimeZoneInfo.ConvertTimeToUtc(datNowLocal).Kind)
Console.WriteLine()

Dim datNowUtc As Date = Date.UtcNow
Console.WriteLine("Converting {0}, Kind {1}", datNowUtc, datNowUtc.Kind)
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNowUtc), TimeZoneInfo.ConvertTimeToUtc(datNowUtc).Kind)
Console.WriteLine()

Dim datNow As Date = CDate("10/26/2007 1:32:00 PM")
Console.WriteLine("Converting {0}, Kind {1}", datNow, datNow.Kind)
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNow), TimeZoneInfo.ConvertTimeToUtc(datNow).Kind)
Console.WriteLine()

Dim datAmbiguous As Date = #11/4/2007 1:30:00AM#    
Console.WriteLine("Converting {0}, Kind {1}, Ambiguous {2}", datAmbiguous, datAmbiguous.Kind, TimeZoneInfo.Local.IsAmbiguousTime(datAmbiguous))
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datAmbiguous), TimeZoneInfo.ConvertTimeToUtc(datAmbiguous).Kind)
Console.WriteLine()

Dim datInvalid As Date = #03/11/2007 2:30:00AM#    
Console.WriteLine("Converting {0}, Kind {1}, Invalid {2}", datInvalid, datInvalid.Kind, TimeZoneInfo.Local.IsInvalidTime(datInvalid))
Try
   Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datInvalid), TimeZoneInfo.ConvertTimeToUtc(datInvalid).Kind)
Catch e As ArgumentException
   Console.WriteLine("   {0}: Cannot convert {1} to UTC.", e.GetType().Name, datInvalid)
End Try
Console.WriteLine()

Dim datNearMax As Date = #12/31/9999 10:00:00PM#
Console.WriteLine("Converting {0}, Kind {1}", datNearMax, datNearMax.Kind)
Console.WriteLine("   ConvertTimeToUtc: {0}, Kind {1}", TimeZoneInfo.ConvertTimeToUtc(datNearMax), TimeZoneInfo.ConvertTimeToUtc(datNearMax).Kind)
Console.WriteLine()
'
' This example produces the following output if the local time zone 
' is Pacific Standard Time:
'
'    Converting 8/31/2007 2:26:28 PM, Kind Local:
'       ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc
'    
'    Converting 8/31/2007 9:26:28 PM, Kind Utc
'       ConvertTimeToUtc: 8/31/2007 9:26:28 PM, Kind Utc
'    
'    Converting 10/26/2007 1:32:00 PM, Kind Unspecified
'       ConvertTimeToUtc: 10/26/2007 8:32:00 PM, Kind Utc
'    
'    Converting 11/4/2007 1:30:00 AM, Kind Unspecified, Ambiguous True
'       ConvertTimeToUtc: 11/4/2007 9:30:00 AM, Kind Utc
'    
'    Converting 3/11/2007 2:30:00 AM, Kind Unspecified, Invalid True
'       ArgumentException: Cannot convert 3/11/2007 2:30:00 AM to UTC.
'    
'    Converting 12/31/9999 10:00:00 PM, Kind Unspecified
'       ConvertTimeToUtc: 12/31/9999 11:59:59 PM, Kind Utc

Commenti

La conversione esatta eseguita dipende dal valore della Kind proprietà del dateTime parametro, come illustrato nella tabella seguente.The exact conversion performed 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 Esegue la conversione dall'ora locale all'ora UTC (Coordinated Universal Time).Converts from local time to Coordinated Universal Time (UTC).
DateTimeKind.Unspecified Presuppone l' dateTime ora locale e converte l'ora locale in ora UTC.Assumes dateTime is local time and converts from local time to UTC.
DateTimeKind.Utc Restituisce dateTime invariato.Returns dateTime unchanged.

Se dateTime corrisponde a un'ora locale ambigua, questo metodo presuppone che si tratta dell'ora locale standard.If dateTime corresponds to an ambiguous local time, this method assumes that it is standard local time. Se dateTime corrisponde a un'ora locale non valida, il metodo genera un'eccezione ArgumentException .If dateTime corresponds to an invalid local time, the method throws an ArgumentException.

Nota

Se il fuso orario locale del computer corrente include più regole di regolazione, questo overload del ConvertTimeToUtc metodo può restituire risultati diversi dai TimeZone.ToUniversalTime DateTime.ToUniversalTime metodi e.If the current computer's local time zone includes multiple adjustment rules, this overload of the ConvertTimeToUtc method can return results that differ from the TimeZone.ToUniversalTime and DateTime.ToUniversalTime methods. TimeZone.ToUniversalTime applica sempre la regola di rettifica corrente alla conversione del fuso orario, indipendentemente dal fatto che dateTime si trovi all'interno dell'intervallo di date.TimeZone.ToUniversalTime always applies the current adjustment rule to time zone conversion, whether or not dateTime lies within its date range. Quando si esegue il .NET Framework 3,5, DateTime.ToUniversalTime applica anche la regola di rettifica corrente alla conversione del fuso orario, indipendentemente dal fatto che dateTime si trovi all'interno dell'intervallo di date.And when executing on .NET Framework 3.5, DateTime.ToUniversalTime also applies the current adjustment rule to time zone conversion, whether or not dateTime lies within its date range.

Se l'equivalente UTC di dateTime è precedente a DateTime.MinValue o successivo DateTime.MaxValue , questo metodo restituisce MinValue rispettivamente o MaxValue .If the UTC equivalent of dateTime is earlier than DateTime.MinValue or later that DateTime.MaxValue, this method returns MinValue or MaxValue, respectively.

Si applica a