views:

4280

answers:

7

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?

+25  A: 

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.

Blair Conrad
an excellent point -- when *storing* dates in a database or file, definitely store them in UTC!
Jeff Atwood
You have to note that when you want to store dates in UTC in the database, you have to make sure that the database doesn't add its own timezone to dates that don't give explicit timezones. Note that DateTime will always use the current timezone when asked for one.
Omer van Kloeten
+10  A: 

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.

Jeff Atwood
Jeff answering my question? Nice! But which post do I mark as answer? This one or Blair's one - they are both informative and make things clear.
Slavo
nice answer jeff!
Raj
+1  A: 

When you need a local time for the machine your application runs at (like CEST for Europe), use Now. If you want a universal time - UtcNow. It's just matter of your preferences - probably making a local website / standalone application you'd want to use the time user has - so affected by his/her timezone setting - DateTime.Now.

Just remember, for a website it's the timezone setting of the server. So if you're displaying the time for the user, either get his prefered timezone and shift the time (just save Utc time to database then, and modify it) or specify it's UTC. If you forget to do so, user can see something like: posted 3 minuses ago and then a time in the future near it :)

kender
+5  A: 

DateTime has no idea what time zones are. It always assumes you're at your local time. UtcNow only means "Subtract my timezone from the time".

If you want to use timezone-aware dates, use DateTimeOffset, which represents a date/time with a timezone. I had to learn that the hard way.

Omer van Kloeten
+8  A: 

One main concept to understand in .NET is that now is now all over the earth no matter what time zone you are in. So if you load a variable with DateTime.Now or DateTime.UtcNow -- the assignment is identical. Your DateTime object knows what timezone you are in and takes that into account regardless of the assignment.

The usefulness of DateTime.UtcNow comes in handy when calculating dates across Daylight Savings Time boundaries. That is, in places that participate in daylight savings time, sometimes there are 25 hours from noon to noon the following day, and sometimes there are 23 hours between noon and noon the following day. If you want to correctly determine the number of hours from time A and time B, you need to first translate each to their UTC equivalents before calculating the TimeSpan.

This is covered by a blog post i wrote that further explains TimeSpan, and includes a link to an even more extensive MS article on the topic.

Carl Camera
+1  A: 

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 time zone adjustments (you can easily verify this with Reflector).

So don't use DateTime.Now for relative time measurements.

Magnus Krisell
+2  A: 

Just a little addition to the points made above: the DateTime struct also contains a little known field called Kind (at least, I did not know about it for a long time). It is basically just a flag indicating whether the time is local or UTC; it does not specify the real offset from UTC for local times. Besides the fact that it indicates with what intentions the stuct was constructed, it also influences the way how the methods ToUniversalTime() and ToLocalTime() work.

Jan Zich