Site logo
Published on

What is the difference between DateTime.UtcNow and DateTime.Now in .NET?

Authors

The difference

DateTime.UtcNow is the time according to the Coordinated Universal Time standard and is the same across timezones. This means that DateTime.UtcNow has the same value in the GMT +2 timezone and in the GMT -7 timezone and all other timezones.

DateTime.Now is the current time in the timezone where the code is executed. This means that DateTime.Now in the GMT +2 timezone will be 2 hours ahead of DateTime.UtcNow.

A time comparison is shown in the table (assuming the day, month, and year remains constant):

TimezoneGMT +0GMT +1GMT +2GMT +3GMT +4GMT +5GMT +6
DateTime.UtcNow12:0012:0012:0012:0012:0012:0012:00
DateTime.Now12:0013:0014:0015:0016:0017:0018:00

When to use DateTime.UtcNow

When storing date and time in a database it is recommended to use DateTime.UtcNow. By doing this, the time and date values will be standardized throughout your app and independent of the timezones your servers or users might be in.

When to use DateTime.Now

When displaying date and time to a user it is recommended to use DateTime.Now. This will ensure that the correct time for the user's timezone is displayed. C# also has a handy extension, .ToLocalTime(), that can be used to convert DateTime.UtcNow to local time.


P.S. - DateTime.Now is slightly slower than DateTime.UtcNow because DateTime.Now uses DateTime.UtcNow internally and calculates the correct time based on the timezone. The difference in performance is negligible for most systems, but if your app does A LOT of time based calculations it is recommended to use DateTime.UtcNow to avoid unnecessary performance overhead.