In this article, we’re going to learn about the difference between DateTime.Now and DateTime.UtcNow in C#. Additionally, on a more general level, we’re going to learn the difference between formatting DateTime values in local time vs in universal time, and also how to check if a date is sent in a local or universal format.
Let’s start.
DateTime.Now and DateTime.UtcNow
What is the difference between DateTime.Now and DateTime.UtcNow?
The property Now
of theDateTime
class returns the current date and time of the machine running the code, expressed in the computer’s local time.
The property UtcNow
of theDateTime
class returns the current date and time of the machine running the code, expressed in UTC format.
UTC is a universal format to represent date and time as an alternative to local time. Also known as the GMT+00 timezone.
Let’s see an example to better understand the difference:
var now = DateTime.Now; var utcNow = DateTime.UtcNow; Console.WriteLine($"Local Now: {now}"); Console.WriteLine($"UTC Now: {utcNow}");
Now, let’s run the app:
Local Now: 1/9/2022 11:52:52 AM UTC Now:Â Â 1/9/2022 09:52:52 AM
The first message on-screen vary by our local time. Each system’s local time depends on the timezone assigned. In most common cases we leave it as the default, a value that fits the geographical area where we reside (in our case UTC+2), but we may also set it to a custom value if needed.
The second message is in the UTC format. It will present the current date and time in UTC, or in the GMT+00 timezone. We can always consider UTC as the base for calculating all the other time zones, like a timezone-neutral format:
Here we can see that the local time of the computer is 2 hours ahead of UTC.
Is DateTime in UTC or Local Time?
When developing applications, we could receive some DateTime
values from external resources we can’t control. We might not know the format used before the transfer.
Let’s say we receive a value as 1/9/2022 09:52:52 AM.
Without knowing much about the sender, this value could be anything – it could be coming from a server in UTC+02, from a UTC-03, or any other time zone as well. In this case, the helpful tool we have is Datetime.Kind
property.
The DateTime.Kind
property indicates if a DateTime
value is represented as Local, UTC, or none. This property returns a value of theDateTimeKind
enum from C#, and its possible values are Local
, Utc
, or Unspecified
.
Let’s see an example of how to check a date’s format:
var now = DateTime.Now; var utcNow = DateTime.UtcNow; Console.WriteLine(now.Kind); Console.WriteLine(utcNow.Kind);
This should display the Local
, then the Utc
kind.
Converting DateTime.Now to DateTime.UtcNow and Vice Versa
After we find out a DateTime
‘s kind, we can decide how to further work with it in our applications. We could convert it from UTC to local if necessary. Or the other way around – from a local kind to UTC. Let’s see how to do these operations.
DateTime.ToLocalTime()
is a method that can take UTC values and turn them into local values. It will choose the timezone of the system currently running the code.
DateTime.ToUniversalTime()
is the method for the reverse operation that we can use to convert a local value into a UTC value:
var now = DateTime.Now; var utcNow = DateTime.UtcNow; if (utcNow.Kind == DateTimeKind.Utc) { var oldKind = utcNow.Kind; var utcToLocal = utcNow.ToLocalTime(); var newKind = utcToLocal.Kind; Console.WriteLine($"Converted {utcNow} from {oldKind} to {newKind}: {utcToLocal}"); } if (now.Kind == DateTimeKind.Local) { var oldKind = now.Kind; var localToUtc = now.ToUniversalTime(); var newKind = localToUtc.Kind; Console.WriteLine($"Converted {now} from {oldKind} to {newKind}: {localToUtc}"); }
The first if
block will convert the UTC value to the local format and show a corresponding message on how the conversion was made.
Reversely, the second if
block will convert the local value to a universal one, and the message will give more details.
Conclusion
In this article, we’ve learned how to use universal and local DateTime formats, what is the difference between them, and also how to check if a DateTime value was created in the universal format or a local format.