DateTimeOffset.ToOffset Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the value of the current DateTimeOffset object to the date and time specified by an offset value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function ToOffset ( _
    offset As TimeSpan _
) As DateTimeOffset
public DateTimeOffset ToOffset(
    TimeSpan offset
)

Parameters

Return Value

Type: System.DateTimeOffset
An 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.

Exceptions

Exception Condition
ArgumentException

The resulting DateTimeOffset object has a DateTime value earlier than MinValue.

-or-

The resulting DateTimeOffset object has a DateTime value later than MaxValue.

ArgumentOutOfRangeException

offset is less than -14 hours.

-or-

offset is greater than 14 hours.

Remarks

The ToOffset method is an alternative to calling the TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) method. 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. 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.

Examples

The following example illustrates how to use the ToOffset method to convert a DateTimeOffset object to a DateTimeOffset object with a different offset.

Module Example
   Private sourceTime As New DateTimeOffset(#9/1/2007 9:30:00 AM#, _
                                            New TimeSpan(-5, 0, 0))

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim targetTime As DateTimeOffset

      ' Convert to same time (return sourceTime unchanged)
      targetTime = sourceTime.ToOffset(New TimeSpan(-5, 0, 0))
      ShowDateAndTimeInfo(outputBlock, targetTime)

      ' Convert to UTC (0 offset)
      targetTime = sourceTime.ToOffset(TimeSpan.Zero)
      ShowDateAndTimeInfo(outputBlock, targetTime)

      ' Convert to 8 hours behind UTC
      targetTime = sourceTime.ToOffset(New TimeSpan(-8, 0, 0))
      ShowDateAndTimeInfo(outputBlock, targetTime)

      ' Convert to 3 hours ahead of UTC
      targetTime = sourceTime.ToOffset(New TimeSpan(3, 0, 0))
      ShowDateAndTimeInfo(outputBlock, targetTime)
   End Sub

   Private Sub ShowDateAndTimeInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal newTime As DateTimeOffset)
      outputBlock.Text &= String.Format("{0} converts to {1}", sourceTime, newTime) & vbCrLf
      outputBlock.Text &= String.Format("{0} and {1} are equal: {2}", _
                        sourceTime, newTime, sourceTime.Equals(newTime)) & vbCrLf
      outputBlock.Text &= String.Format("{0} and {1} are identical: {2}", _
                        sourceTime, newTime, _
                        sourceTime.EqualsExact(newTime)) & vbCrLf
      outputBlock.Text &= vbCrLf
   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
using System;

public class Example
{
   private static DateTimeOffset sourceTime;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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(outputBlock, targetTime);

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

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

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

   private static void ShowDateAndTimeInfo(System.Windows.Controls.TextBlock outputBlock, DateTimeOffset newTime)
   {
      outputBlock.Text += String.Format("{0} converts to {1}", sourceTime, newTime) + "\n";
      outputBlock.Text += String.Format("{0} and {1} are equal: {2}",
                        sourceTime, newTime, sourceTime.Equals(newTime)) + "\n";
      outputBlock.Text += String.Format("{0} and {1} are identical: {2}",
                        sourceTime, newTime,
                        sourceTime.EqualsExact(newTime)) + "\n";
      outputBlock.Text += "\n";
   }
}
//
// 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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.