The reason this code throws an exception for you is that you're presumably in a time zone which is ahead of UTC - so when DateTime.MinValue
(which has a Kind
of Unspecified
) is converted to UTC, it's becoming invalid. The conversion is specified in the documentation:
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.
You could just specify the offset explicitly though:
DateTimeOffset d = new DateTimeOffset(DateTime.MinValue, TimeSpan.Zero);
That won't result in any conversion... but I believe it's exactly equivalen to default(DateTimeOffset)
. (It's more explicit, mind you - often a good thing.)
You might also want to consider using DateTimeOffset?
and specifying null
for the absence of a "real" value, instead of just using a dummy value.