DateTimeOffset Constructor (DateTime, TimeSpan)

DateTimeOffset Constructor (DateTime, TimeSpan)

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

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

public DateTimeOffset(
	DateTime dateTime,
	TimeSpan offset
)

Parameters

dateTime
Type: System.DateTime
A date and time.
offset
Type: System.TimeSpan
The time's offset from Coordinated Universal Time (UTC).

ExceptionCondition
ArgumentException

dateTime.Kind equals Utc and offset does not equal zero.

-or-

dateTime.Kind equals Local and offset does not equal the offset of the system's local time zone.

-or-

offset is not specified in whole minutes.

ArgumentOutOfRangeException

offset is less than -14 hours or greater than 14 hours.

-or-

UtcDateTime is less than MinValue or greater than MaxValue.

This constructor's behavior depends in part on the value of the Kind property of the dateTime parameter:

  • If the value of Kind is DateTimeKind.Utc, the value of the offset parameter must be 0 or an ArgumentException is thrown.

  • If the value of Kind is DateTimeKind.Local, the value of the offset parameter must be equal to the local time zone's offset from Coordinated Universal Time (UTC) for that particular date or an ArgumentException is thrown.

  • If the value of Kind is DateTimeKind.Unspecified, the offset parameter can have any valid value.

The following example shows how to initialize a DateTimeOffset object with a date and time and the offset of the local time zone when that time zone is not known in advance.


DateTime localTime = new DateTime(2007, 07, 12, 06, 32, 00); 
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime, 
                         TimeZoneInfo.Local.GetUtcOffset(localTime));
Console.WriteLine(dateAndOffset);
// The code produces the following output:
//    7/12/2007 6:32:00 AM -07:00


.NET Framework

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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2016 Microsoft