7

In designing web applications especially using Asp.net MVC4 c#, once I use

  private DateTime currentDateTime = DateTime.Now;

somewhere in my code and save currentDateTime in a database; and then, I deploy my application in the web,

What will be the value of currentDateTime?

a. DateTime of the browser/computer which opened my application (timezone issue)

b. DateTime of the server in which I deployed my application (all values are in under one timezone)

Sorry, but I am just starting in web applications so this one confuses me. Hope you can clarify it to me. Thanks.

raberana
  • 11,739
  • 18
  • 69
  • 95

5 Answers5

8

The value will the local time on the server.

However, I would strongly recommend using Universal Coordinated Time (UTC) eg, DateTime.UtcNow, it makes reasoning about date times much simpler and will mean that you will be able to host your application anywhere in the world without adverse affects.

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • Excellent advice. Keep in mind this will still require proper time & timezone information to be set on your servers. (This comes from the voice of experience. I've had to debug a server side issue only to discover later we were chasing the wrong issue -- because the time logged for various events was incorrect). – Dan Esparza Dec 26 '12 at 17:15
7

The ASP.Net code is executing on the server, so it will be server time.

mellamokb
  • 56,094
  • 12
  • 110
  • 136
5

The correct answer is:

b. DateTime of the server in which I deployed my application, as that's where the code is executing.

There is another DateTime property, DateTime.UtcNow that always gets the UTC time.
I would recommend using that one both in the application and in the database (getutcdate() ins sql server, for example), as it avoids all kinds of synchronicity issues.

If you need to show the date to the client, just offset the saved time by the client time zone, and you're golden.

SWeko
  • 30,434
  • 10
  • 71
  • 106
5

It will be the time on the server, since that's where the code is executing.

You may want to use DateTime.UtcNow instead, as this reduces time zone issues. You can easily convert to the local time on the client or server when you go to display or evaluate the time.

Jon B
  • 51,025
  • 31
  • 133
  • 161
2

It will be the time on the server.

Dave Zych
  • 21,581
  • 7
  • 51
  • 66