Share via


DateTimeOffset Implicit Conversion (DateTime to DateTimeOffset)

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

Defines an implicit conversion of a DateTime object to a DateTimeOffset object.

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

Syntax

'Declaration
Public Shared Widening Operator CType ( _
    dateTime As DateTime _
) As DateTimeOffset
public static implicit operator DateTimeOffset (
    DateTime dateTime
)

Parameters

Return Value

Type: System.DateTimeOffset
The converted object.

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

The Implicit method enables the compiler to automatically convert a DateTime object to a DateTimeOffset object without an explicit casting operator (in C#) or a call to a conversion function (in Visual Basic). It defines a widening conversion that does not involve data loss and does not throw an OverflowException. The Implicit method makes code such as the following possible:

Dim timeWithOffset As DateTimeOffset
timeWithOffset = #7/3/2008 6:45:00 PM#
outputBlock.Text &= timeWithOffset.ToString() & vbCrLf

timeWithOffset = Date.UtcNow
outputBlock.Text &= timeWithOffset.ToString() & vbCrLf

timeWithOffset = Date.SpecifyKind(Date.Now, DateTimeKind.Unspecified)
outputBlock.Text &= timeWithOffset.ToString() & vbCrLf

timeWithOffset = #1/1/2008 2:30:00 AM# + New TimeSpan(1, 0, 0, 0)
outputBlock.Text &= timeWithOffset.ToString() & vbCrLf
' The example produces the following output if run on 3/20/2007 
' at 6:25 PM on a computer in the U.S. Pacific Daylight Time zone:
'       7/3/2008 6:45:00 PM -07:00
'       3/21/2007 1:25:52 AM +00:00
'       3/20/2007 6:25:52 PM -07:00
'       1/2/2008 2:30:00 AM -08:00      
DateTimeOffset timeWithOffset;
timeWithOffset = new DateTime(1008, 7, 3, 18, 45, 0);
outputBlock.Text += timeWithOffset.ToString() + "\n";

timeWithOffset = DateTime.UtcNow;
outputBlock.Text += timeWithOffset.ToString() + "\n";

timeWithOffset = DateTime.SpecifyKind(DateTime.Now,
                                      DateTimeKind.Unspecified);
outputBlock.Text += timeWithOffset.ToString() + "\n";

timeWithOffset = new DateTime(2008, 1, 1, 2, 30, 0) +
                 new TimeSpan(1, 0, 0, 0);
outputBlock.Text += timeWithOffset.ToString() + "\n";
// The example produces the following output if run on 3/20/2007 
// at 6:25 PM on a computer in the U.S. Pacific Daylight Time zone:
//       7/3/2008 6:45:00 PM -07:00
//       3/21/2007 1:25:52 AM +00:00
//       3/20/2007 6:25:52 PM -07:00
//       1/2/2008 2:30:00 AM -08:00      

This method is equivalent to the DateTimeOffset constructor. The offset of the resulting DateTimeOffset object depends on the value of the DateTime.Kind property of the dateTime parameter:

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.