DateTimeOffset.ToOffset(TimeSpan) DateTimeOffset.ToOffset(TimeSpan) DateTimeOffset.ToOffset(TimeSpan) DateTimeOffset.ToOffset(TimeSpan) Method

定義

現在の値に変換DateTimeOffsetに日付と時刻のオフセットの値で指定されたオブジェクト。Converts the value of the current DateTimeOffset object to the date and time specified by an offset value.

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

パラメーター

offset
TimeSpan TimeSpan TimeSpan TimeSpan

変換するオフセット、DateTimeOffset値します。The offset to convert the DateTimeOffset value to.

戻り値

元と同じであるオブジェクトDateTimeOffsetオブジェクト (つまり、そのToUniversalTime()メソッドが時間内に同じ時点を返します) が持つOffsetプロパティに設定されてoffsetAn object that is equal to the original DateTimeOffset object (that is, their ToUniversalTime() methods return identical points in time) but whose Offset property is set to offset.

例外

その結果、DateTimeOffsetオブジェクトには、DateTime値よりも前MinValueThe resulting DateTimeOffset object has a DateTime value earlier than MinValue.

- または --or- 結果のDateTimeOffsetオブジェクトには、DateTime値よりも後MaxValueします。The resulting DateTimeOffset object has a DateTime value later than MaxValue.

offset -14 時間未満です。offset is less than -14 hours.

- または --or- offset 14 時間を超えています。offset is greater than 14 hours.

次の例を使用する方法を示しています、ToOffsetに変換するメソッド、DateTimeOffsetオブジェクトをDateTimeOffsetさまざまなオフセットを含むオブジェクト。The following example illustrates how to use the ToOffset method to convert a DateTimeOffset object to a DateTimeOffset object with a different offset.

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
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

注釈

ToOffsetメソッドを呼び出す代わりに、TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo)メソッド。The ToOffset method is an alternative to calling the TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) method. 世界協定時刻 (UTC) からのタイム ゾーン オフセットがわかっている場合は、1 つのタイム ゾーンから別の単純な変換を実行するために便利ですができます。It can be useful for performing simple conversions from one time zone to another when the time zones' offsets from Coordinated Universal Time (UTC) are known. ただし、ため、どちらも、元DateTimeOffsetオブジェクトも、新しいDateTimeOffsetメソッドの呼び出しによって返されるオブジェクトは明確に関連する特定のタイム ゾーン、メソッドは、変換のタイム ゾーン調整規則が適用されません。However, because neither the original DateTimeOffset object nor the new DateTimeOffset object returned by the method call are unambiguously related to a particular time zone, the method does not apply any time zone adjustment rules in the conversion.

適用対象