Another commonly-used property of DateTime
is UtcNow even though it’s not used as commonly as
the Now property. This property returns the current
date and time in UTC. This property is designed to serve for many places where Now is being misused and I listed some of the highlights in
the previous section.
In general, if you’re going to store DateTime
values in database or perform calculations on such values, it’s better to use UtcNow because in the former case, this helps you have a
universal value regardless of the local time of the machine where you host your
program and in the latter case there is no difference between the duration of
time calculated by Now and UtcNow.
As you can see below, UtcNow has a
simpler implementation than Now (listing 2) and in
fact, Now is written based on UtcNow
with some additions.
Listing 2: Internal implementation of
public static DateTime UtcNow
[TargetedPatchingOptOut("Performance critical to inline across NGen
image boundaries"), SecuritySafeCritical]
long systemTimeAsFileTime = DateTime.GetSystemTimeAsFileTime();
return new DateTime((ulong)(systemTimeAsFileTime +
504911232000000000L | 4611686018427387904L));