Share via


DateTimeOffset Constructor (DateTime)

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

Initializes a new instance of the DateTimeOffset structure using the specified DateTime value.

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

Syntax

'Declaration
Public Sub New ( _
    dateTime As DateTime _
)
public DateTimeOffset(
    DateTime dateTime
)

Parameters

Exceptions

Exception Condition
ArgumentOutOfRangeException

The Coordinated Universal Time (UTC) date and time that results from applying the offset is earlier than MinValue.

-or-

The UTC date and time that results from applying the offset is later than MaxValue.

Remarks

This constructor's behavior depends on the value of the DateTime.Kind property of the dateTime parameter:

Version Notes

XNA Framework

 When this constructor is used in the XNA Framework, it throws a NotSupportedException exception.

Examples

The following example illustrates how the value of the DateTime.Kind property of the dateTime parameter affects the date and time value that is returned by this constructor.

Dim localNow As Date = Date.Now
Dim localOffset As New DateTimeOffset(localNow)
outputBlock.Text &= localOffset.ToString() & vbCrLf

Dim utcNow As Date = Date.UtcNow
Dim utcOffset As New DateTimeOffset(utcNow)
outputBlock.Text &= utcOffset.ToString() & vbCrLf

Dim unspecifiedNow As Date = Date.SpecifyKind(Date.Now, _
                                  DateTimeKind.Unspecified)
Dim unspecifiedOffset As New DateTimeOffset(unspecifiedNow)
outputBlock.Text &= unspecifiedOffset.ToString() & vbCrLf
'
' The code produces the following output if run on Feb. 23, 2007, on
' a system 8 hours earlier than UTC:
'    2/23/2007 4:21:58 PM -08:00
'    2/24/2007 12:21:58 AM +00:00
'    2/23/2007 4:21:58 PM -08:00      
DateTime localNow = DateTime.Now;
DateTimeOffset localOffset = new DateTimeOffset(localNow);
outputBlock.Text += localOffset.ToString() + "\n";

DateTime utcNow = DateTime.UtcNow;
DateTimeOffset utcOffset = new DateTimeOffset(utcNow);
outputBlock.Text += utcOffset.ToString() + "\n";

DateTime unspecifiedNow = DateTime.SpecifyKind(DateTime.Now,
                               DateTimeKind.Unspecified);
DateTimeOffset unspecifiedOffset = new DateTimeOffset(unspecifiedNow);
outputBlock.Text += unspecifiedOffset.ToString() + "\n";
//
// The code produces the following output if run on Feb. 23, 2007, on
// a system 8 hours earlier than UTC:
//   2/23/2007 4:21:58 PM -08:00
//   2/24/2007 12:21:58 AM +00:00
//   2/23/2007 4:21:58 PM -08: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.