Partager via


DateTimeOffset Constructor (Int64, TimeSpan)

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

Initializes a new instance of the DateTimeOffset structure using the specified number of ticks and offset.

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

Syntax

'Declaration
Public Sub New ( _
    ticks As Long, _
    offset As TimeSpan _
)
public DateTimeOffset(
    long ticks,
    TimeSpan offset
)

Parameters

  • ticks
    Type: System.Int64
    A date and time expressed as the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight on January 1, 0001.
  • offset
    Type: System.TimeSpan
    The time's offset from Coordinated Universal Time (UTC).

Exceptions

Exception Condition
ArgumentException

offset is not specified in whole minutes.

ArgumentOutOfRangeException

The UtcDateTime property is earlier than MinValue or later than MaxValue.

-or-

ticks is less than DateTimeOffset.MinValue.Ticks or greater than DateTimeOffset.MaxValue.Ticks.

-or-

Offset s less than -14 hours or greater than 14 hours.

Remarks

Ordinarily, trying to call the DateTimeOffset constructor to instantiate a DateTimeOffset value with a local time and an offset other than that of the local time zone throws an ArgumentException. You can use this overload of the DateTimeOffset constructor to work around this limitation. The following example uses the local time's number of ticks to instantiate a DateTimeOffset value whose offset does not necessarily represent that of the local time:

Dim localTime As Date = Date.Now
Dim nonLocalDateWithOffset As New DateTimeOffset(localTime.Ticks, _
                                  New TimeSpan(2, 0, 0))
outputBlock.Text &= nonLocalDateWithOffset.ToString() & vbCrLf
'
' The code produces the following output if run on Feb. 23, 2007:
'    2/23/2007 4:37:50 PM +02:00
DateTime localTime = DateTime.Now;
DateTimeOffset nonLocalDateWithOffset = new DateTimeOffset(localTime.Ticks,
                                  new TimeSpan(2, 0, 0));
outputBlock.Text += nonLocalDateWithOffset + "\n";
//
// The code produces the following output if run on Feb. 23, 2007:
//    2/23/2007 4:37:50 PM +02:00

Examples

The following example initializes a DateTimeOffset object by using the number of ticks in an arbitrary date (in this case, July 16, 2007, at 1:32 PM) with an offset of -5.

Dim dateWithoutOffset As Date = #7/16/2007 1:32:00 PM#
Dim timeFromTicks As New DateTimeOffset(dateWithoutOffset.Ticks, _
                         New TimeSpan(-5, 0, 0))
outputBlock.Text &= timeFromTicks.ToString() & vbCrLf
' The code produces the following output:
'    7/16/2007 1:32:00 PM -05:00
DateTime dateWithoutOffset = new DateTime(2007, 7, 16, 13, 32, 00);
DateTimeOffset timeFromTicks = new DateTimeOffset(dateWithoutOffset.Ticks,
                               new TimeSpan(-5, 0, 0));
outputBlock.Text += timeFromTicks.ToString() + "\n";
// The code produces the following output:
//    7/16/2007 1:32:00 PM -05:00

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.