I need to upgrade some piece of code from statically calling DateTime.UtcNow
to calling a time provider service which returns, basically, DateTimeOffset.UtcNow
. To further convert this DateTimeOffset
instance to DateTime
, there is the DateTime
property. Alternatively, it seems that there is an implicit conversion from DateTimeOffset
to DateTime
.
I'm a bit concerned that there might be some edge cases that I can't see right now where these two would not be equivalent. Are there?
If you look at the value of DateTimeOffset.UtcNow.DateTime.Kind
you will see the it is Unspecified. Unspecified kinds are treated as local times by the framework. The kind of DateTime.UtcNow
is Utc, so there will be differences when timezone conversions are applied to and from the local timezone.
The work around is to use the DateTimeOffset.UtcNow.UtcDateTime
which has the Utc kind specified.