Convert DateTime.MinValue to DateTimeOffset

I am trying to convert DateTime.MinValue to a DateTimeOffset value, but getting an ArgumentOutOfRange exception.

I watched an MSDN article on implicit conversions of DateTime to DateTimeOffset , and the Exception section states that I will get this ArgumentOutOfRange exception when;

... Date and time of coordinated universal time (UTC) that occurs as a result of applying an offset earlier than MinValue ....

Why then the following code throws an exception:

DateTime test = DateTime.MinValue;
DateTimeOffset dto = test;

Is it just because of my time zone? I am in GMT +8, but my understanding of the above code is that the test is created using Unspecified.

I am working on a problem by simply testing the MinValue of my DateTime, and if so, use DateTimeOffset.MinValue instead.

I'm just wondering why my undefined view of a DateTime object is causing an error.

+5
source share
1 answer

If you are in GMT + 8, local time DateTime.MinValuecorresponds to UTC earlier than DateTime.MinValue, therefore an exception. From the documentation you refer to:

If the value of the DateTime.Kind property is DateTimeKind.Local or DateTimeKind.Unspecified, the date and time of the DateTimeOffset object are set to dateTime, and its Offset property is set to the offset of the current local time zone system.

, DateTime of MinValue Offset 8 , , / UTC, , , .

( , UTC , UTC. Noda Time , Offset, LocalInstant Instant ...)

+5

All Articles