I would like to set default value to DateTimeOffset - it should not be DateTime.Now but rather DateTime.MinValue or default(DateTime)
Any thoughts how can I do it? This code:
DateTimeOffset d = new DateTimeOffset(DateTime.MinValue)
throws an exception
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
isDateTimeKind.Local
orDateTimeKind.Unspecified
, theDateTime
property of the new instance is set equal todateTime
, and theOffset
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.