Export (0) Print
Expand All

DateTimeOffset Implicit Conversion (DateTime to DateTimeOffset)

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

Namespace:  System
Assemblies:   System.Runtime (in System.Runtime.dll)
  mscorlib (in mscorlib.dll)

public static implicit operator DateTimeOffset (
	DateTime dateTime
)

Parameters

dateTime
Type: System.DateTime

The object to convert.

Return Value

Type: System.DateTimeOffset
The converted object.

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

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:

DateTimeOffset timeWithOffset; 
timeWithOffset = new DateTime(1008, 7, 3, 18, 45, 0);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.UtcNow;
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = DateTime.SpecifyKind(DateTime.Now, 
                                      DateTimeKind.Unspecified);
Console.WriteLine(timeWithOffset.ToString());

timeWithOffset = new DateTime(2008, 1, 1, 2, 30, 0) + 
                 new TimeSpan(1, 0, 0, 0);
Console.WriteLine(timeWithOffset.ToString());
// 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:

.NET Framework

Supported in: 4.6, 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

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

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.

Show:
© 2014 Microsoft