Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between DateTime.UtcNow and DateTime.Now.ToUniversalTime()

Tags:

c#

To me they're both the same. Is UtcNow simply a shortcut?

like image 899
Anthony Avatar asked Aug 31 '10 08:08

Anthony


People also ask

What is the difference between DateTime UtcNow and DateTime now?

UtcNow tells you the date and time as it would be in Coordinated Universal Time, which is also called the Greenwich Mean Time time zone - basically like it would be if you were in London England, but not during the summer. DateTime. Now gives the date and time as it would appear to someone in your current locale.

What is ToUniversalTime in C#?

The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.

Does DateTime return UTC?

Since our system is running on Ubuntu 20.04 Linux server, from our tests, we found that all DateTime. Now call will return a UTC time, not local time.

What is DateTime now in C#?

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time. public: static property DateTime Now { DateTime get(); }; C# Copy.


2 Answers

Actually it's the other way around. The Now property is implemented as:

public static DateTime Now {   get {     return UtcNow.ToLocalTime();   } } 
like image 158
Guffa Avatar answered Oct 08 '22 19:10

Guffa


There is a long example in the Documentation for UtcNow which shows them to be the same.

like image 30
Jamiec Avatar answered Oct 08 '22 17:10

Jamiec