DateTimeOffset.ToLocalTime DateTimeOffset.ToLocalTime DateTimeOffset.ToLocalTime DateTimeOffset.ToLocalTime Method

Definizione

Converte l'oggetto DateTimeOffset corrente in un oggetto DateTimeOffset che rappresenta l'ora locale.Converts the current DateTimeOffset object to a DateTimeOffset object that represents the local time.

public:
 DateTimeOffset ToLocalTime();
public DateTimeOffset ToLocalTime ();
member this.ToLocalTime : unit -> DateTimeOffset
Public Function ToLocalTime () As DateTimeOffset

Restituisce

Oggetto che rappresenta la data e l'ora dell'oggetto DateTimeOffset corrente convertite nell'ora locale.An object that represents the date and time of the current DateTimeOffset object converted to local time.

Esempi

Nell'esempio seguente viene usato ToLocalTime il metodo per convertire DateTimeOffset un valore nell'ora locale nel fuso orario standard del Pacifico.The following example uses the ToLocalTime method to convert a DateTimeOffset value to local time in the Pacific Standard Time zone. Viene inoltre illustrato il supporto del metodo per le regole di rettifica del fuso orario locale.It also illustrates the method's support for the local time zone's adjustment rules.

// Local time changes on 3/11/2007 at 2:00 AM
DateTimeOffset originalTime, localTime;

originalTime = new DateTimeOffset(2007, 3, 11, 3, 0, 0, 
                                  new TimeSpan(-6, 0, 0));
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), 
                                           localTime.ToString());   

originalTime = new DateTimeOffset(2007, 3, 11, 4, 0, 0, 
                                  new TimeSpan(-6, 0, 0));
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), 
                                           localTime.ToString());    

// Define a summer UTC time
originalTime = new DateTimeOffset(2007, 6, 15, 8, 0, 0, 
                                  TimeSpan.Zero);
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(),
                                           localTime.ToString());    

// Define a winter time
originalTime = new DateTimeOffset(2007, 11, 30, 14, 0, 0, 
                                  new TimeSpan(3, 0, 0));
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), 
                                           localTime.ToString());
// The example produces the following output:
//    Converted 3/11/2007 3:00:00 AM -06:00 to 3/11/2007 1:00:00 AM -08:00.
//    Converted 3/11/2007 4:00:00 AM -06:00 to 3/11/2007 3:00:00 AM -07:00.
//    Converted 6/15/2007 8:00:00 AM +00:00 to 6/15/2007 1:00:00 AM -07:00.
//    Converted 11/30/2007 2:00:00 PM +03:00 to 11/30/2007 3:00:00 AM -08:00.                                                           
' Local time changes on 3/11/2007 at 2:00 AM
Dim originalTime, localTime As DateTimeOffset

originalTime = New DateTimeOffset(#03/11/2007 3:00AM#, _
                                  New TimeSpan(-6, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
                                           localTime.ToString())       

originalTime = New DateTimeOffset(#03/11/2007 4:00AM#, _
                                  New TimeSpan(-6, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
                                           localTime.ToString())       

' Define a summer UTC time
originalTime = New DateTimeOffset(#6/15/2007 8:00AM#, _
                                  TimeSpan.Zero)
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
                                           localTime.ToString())       

' Define a winter time
originalTime = New DateTimeOffset(#11/30/2007 2:00PM#, _
                                  New TimeSpan(3, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
                                           localTime.ToString())   
' The example produces the following output:
'    Converted 3/11/2007 3:00:00 AM -06:00 to 3/11/2007 1:00:00 AM -08:00.
'    Converted 3/11/2007 4:00:00 AM -06:00 to 3/11/2007 3:00:00 AM -07:00.
'    Converted 6/15/2007 8:00:00 AM +00:00 to 6/15/2007 1:00:00 AM -07:00.
'    Converted 11/30/2007 2:00:00 PM +03:00 to 11/30/2007 3:00:00 AM -08:00.                                                           

Commenti

Quando si esegue la conversione nell'ora locale, il metodo converte prima di DateTimeOffset tutto la data e l'ora dell'oggetto corrente nell'ora UTC (Coordinated Universal Time) sottraendo l'offset dal tempo.In performing the conversion to local time, the method first converts the current DateTimeOffset object's date and time to Coordinated Universal Time (UTC) by subtracting the offset from the time. Converte quindi la data e l'ora UTC nell'ora locale aggiungendo la differenza di fuso orario locale.It then converts the UTC date and time to local time by adding the local time zone offset. In tal caso, prende in considerazione le regole di rettifica per il fuso orario locale.In doing this, it takes account of any adjustment rules for the local time zone.

Il valore dell'oggetto corrente DateTimeOffset e il valore DateTimeOffset dell'oggetto restituito dalla chiamata al metodo rappresentano lo stesso punto nel tempo.Both the value of the current DateTimeOffset object and the value of the DateTimeOffset object returned by the method call represent the same point in time. Ovvero, se entrambi vengono passati al DateTimeOffset.Equals(DateTimeOffset, DateTimeOffset) metodo, il metodo restituirà. trueThat is, if both are passed to the DateTimeOffset.Equals(DateTimeOffset, DateTimeOffset) method, the method will return true.

Se la conversione causa un periodo di tempo non compreso nell'intervallo del DateTimeOffset tipo, il ToLocalTime metodo restituisce un DateTimeOffset oggetto con la data e l'ora impostate su MaxValue o MinValue e l'offset impostato sulla differenza di fuso orario locale.If the conversion causes a time that is out of range of the DateTimeOffset type, the ToLocalTime method returns a DateTimeOffset object that has the date and time set to either MaxValue or MinValue and the offset set to the local time zone offset.

Si applica a

Vedi anche