DateTime.Now vs. DateTime.UtcNow
I've been wondering what exactly are the principles of how the two properties work. I know the second one is universal and basically doesn't deal with time zones, but can someone explain in detail how they work and which one should be used in what scenario?
Solution 1:
DateTime.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.
I'd recommend using DateTime.Now
whenever you're displaying a date to a human being - that way they're comfortable with the value they see - it's something that they can easily compare to what they see on their watch or clock. Use DateTime.UtcNow
when you want to store dates or use them for later calculations that way (in a client-server model) your calculations don't become confused by clients in different time zones from your server or from each other.
Solution 2:
It's really quite simple, so I think it depends what your audience is and where they live.
If you don't use Utc, you must know the timezone of the person you're displaying dates and times to -- otherwise you will tell them something happened at 3 PM in system or server time, when it really happened at 5 PM where they happen to live.
We use DateTime.UtcNow
because we have a global web audience, and because I'd prefer not to nag every user to fill out a form indicating what timezone they live in.
We also display relative times (2 hours ago, 1 day ago, etc) until the post ages enough that the time is "the same" no matter where on Earth you live.
Solution 3:
Also note the performance difference; DateTime.UtcNow
is somewhere around 30 times faster then DateTime.Now
, because internally DateTime.Now
is doing a lot of timezone adjustments (you can easily verify this with Reflector).
So do NOT use DateTime.Now
for relative time measurements.