DateTimeOffset.ToOffset Method

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)

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

Parameters

offset
Type: System.TimeSpan

The offset to convert the DateTimeOffset value to.

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.

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

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.

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

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5 SP1, 3.0 SP1, 2.0 SP1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft