DateTimeOffset Constructor (DateTime)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes a new instance of the DateTimeOffset structure using the specified DateTime value.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- dateTime
- Type: System.DateTime
A date and time.
| 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. |
This constructor's behavior depends on the value of the DateTime.Kind property of the dateTime parameter:
If the value of DateTime.Kind is DateTimeKind.Utc, the DateTime property of the new instance is set equal to dateTime, and the Offset property is set equal to Zero.
If the value of DateTime.Kind is DateTimeKind.Local or DateTimeKind.Unspecified, the DateTime property of the new instance is set equal to dateTime, and the Offset property is set equal to the offset of the local system's current time zone.
Version Notes
XNA Framework
When this constructor is used in the XNA Framework, it throws a NotSupportedException exception.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.
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