Condividi tramite


DateTimeOffset.ToOffset(TimeSpan) Metodo

Definizione

Converte il valore dell'oggetto DateTimeOffset corrente nella data e nell'ora specificate da un valore di offset.

public:
 DateTimeOffset ToOffset(TimeSpan offset);
public DateTimeOffset ToOffset (TimeSpan offset);
member this.ToOffset : TimeSpan -> DateTimeOffset
Public Function ToOffset (offset As TimeSpan) As DateTimeOffset

Parametri

offset
TimeSpan

Offset nel quale convertire il valore DateTimeOffset.

Restituisce

Oggetto uguale all'oggetto DateTimeOffset originale (vale a dire i cui metodi ToUniversalTime() restituiscono date e ore identiche) ma la cui proprietà Offset è impostata su offset.

Eccezioni

L'oggetto risultante DateTimeOffset ha un DateTime valore precedente a DateTimeOffset.MinValue.

-oppure-

L'oggetto risultante DateTimeOffset ha un DateTime valore successivo a DateTimeOffset.MaxValue.

offset è minore di -14 ore.

-oppure-

offset è maggiore di 14 ore.

Esempio

Nell'esempio seguente viene illustrato come utilizzare il ToOffset metodo per convertire un DateTimeOffset oggetto in un oggetto con un DateTimeOffset offset diverso.

using System;

public class DateTimeOffsetConversion
{
   private static DateTimeOffset sourceTime;

   public static void Main()
   {
      DateTimeOffset targetTime;
      sourceTime = new DateTimeOffset(2007, 9, 1, 9, 30, 0,
                                      new TimeSpan(-5, 0, 0));

      // Convert to same time (return sourceTime unchanged)
      targetTime = sourceTime.ToOffset(new TimeSpan(-5, 0, 0));
      ShowDateAndTimeInfo(targetTime);

      // Convert to UTC (0 offset)
      targetTime = sourceTime.ToOffset(TimeSpan.Zero);
      ShowDateAndTimeInfo(targetTime);

      // Convert to 8 hours behind UTC
      targetTime = sourceTime.ToOffset(new TimeSpan(-8, 0, 0));
      ShowDateAndTimeInfo(targetTime);

      // Convert to 3 hours ahead of UTC
      targetTime = sourceTime.ToOffset(new TimeSpan(3, 0, 0));
      ShowDateAndTimeInfo(targetTime);
   }

   private static void ShowDateAndTimeInfo(DateTimeOffset newTime)
   {
      Console.WriteLine("{0} converts to {1}", sourceTime, newTime);
      Console.WriteLine("{0} and {1} are equal: {2}",
                        sourceTime, newTime, sourceTime.Equals(newTime));
      Console.WriteLine("{0} and {1} are identical: {2}",
                        sourceTime, newTime,
                        sourceTime.EqualsExact(newTime));
      Console.WriteLine();
   }
}
//
// The example displays the following output:
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False
open System

let sourceTime = DateTimeOffset(2007, 9, 1, 9, 30, 0, TimeSpan(-5, 0, 0))

let showDateAndTimeInfo newTime =
    printfn $"{sourceTime} converts to {newTime}"
    printfn $"{sourceTime} and {newTime} are equal: {sourceTime.Equals newTime}"
    printfn $"{sourceTime} and {newTime} are identical: {sourceTime.EqualsExact newTime}\n"

[<EntryPoint>]
let main _ =
    // Convert to same time (return sourceTime unchanged)
    let targetTime = sourceTime.ToOffset(TimeSpan(-5, 0, 0))
    showDateAndTimeInfo targetTime

    // Convert to UTC (0 offset)
    let targetTime = sourceTime.ToOffset TimeSpan.Zero
    showDateAndTimeInfo targetTime

    // Convert to 8 hours behind UTC
    let targetTime = sourceTime.ToOffset(TimeSpan(-8, 0, 0))
    showDateAndTimeInfo targetTime

    // Convert to 3 hours ahead of UTC
    let targetTime = sourceTime.ToOffset(TimeSpan(3, 0, 0))
    showDateAndTimeInfo targetTime

    0

// The example displays the following output:
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
//
//    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
//    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False
Module DateTimeOffsetConversion
   Private sourceTime As New DateTimeOffset(#9/1/2007 9:30AM#, _
                                            New TimeSpan(-5, 0, 0))
   
   Public Sub Main()
      Dim targetTime As DateTimeOffset
      
      ' Convert to same time (return sourceTime unchanged)
      targetTime = sourceTime.ToOffset(New TimeSpan(-5, 0, 0))
      ShowDateAndTimeInfo(targetTime)
      
      ' Convert to UTC (0 offset)
      targetTime = sourceTime.ToOffset(TimeSpan.Zero)
      ShowDateAndTimeInfo(targetTime)
      
      ' Convert to 8 hours behind UTC
      targetTime = sourceTime.ToOffset(New TimeSpan(-8, 0, 0))
      ShowDateAndTimeInfo(targetTime)
      
      ' Convert to 3 hours ahead of UTC
      targetTime = sourceTime.ToOffset(New TimeSpan(3, 0, 0))
      ShowDateAndTimeInfo(targetTime)
   End Sub
   
   Private Sub ShowDateAndTimeInfo(newTime As DateTimeOffset)
      Console.WriteLine("{0} converts to {1}", sourceTime, newTime)
      Console.WriteLine("{0} and {1} are equal: {2}", _
                        sourceTime, newTime, sourceTime.Equals(newTime))
      Console.WriteLine("{0} and {1} are identical: {2}", _
                        sourceTime, newTime, _
                        sourceTime.EqualsExact(newTime)) 
      Console.WriteLine()
   End Sub
End Module
'
' The example displays the following output:
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 9:30:00 AM -05:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 9:30:00 AM -05:00 are identical: True
'    
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 2:30:00 PM +00:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 2:30:00 PM +00:00 are identical: False
'    
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 6:30:00 AM -08:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 6:30:00 AM -08:00 are identical: False
'    
'    9/1/2007 9:30:00 AM -05:00 converts to 9/1/2007 5:30:00 PM +03:00
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are equal: True
'    9/1/2007 9:30:00 AM -05:00 and 9/1/2007 5:30:00 PM +03:00 are identical: False

Commenti

Il ToOffset metodo è un'alternativa alla chiamata al TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) metodo . Può essere utile per eseguire conversioni semplici da un fuso orario a un altro quando sono noti gli offset dei fusi orari dall'ora UTC (Coordinated Universal Time). Tuttavia, poiché né l'oggetto originale DateTimeOffset né il nuovo DateTimeOffset oggetto restituito dalla chiamata al metodo sono correlati in modo univoco a un determinato fuso orario, il metodo non applica regole di regolazione del fuso orario nella conversione.

Si applica a