Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which time does DateTime.Now really gets in web applications?

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.

like image 909
raberana Avatar asked Dec 26 '12 15:12

raberana


People also ask

What timezone does DateTime now return?

The property UtcNow of the DateTime class returns the current date and time of the machine running the code, expressed in UTC format. UTC is a universal format to represent date and time as an alternative to local time. Also known as the GMT+00 timezone.

Does DateTime now use local time?

Mind that datetime. today() and datetime. now() return the local time, not the UTC time, so applying .

Does DateTime now give UTC?

It also sets Kind to Utc . DateTime. Now alters the Ticks value to what it would be if it was your time of day in the GMT time zone.

How accurate is DateTime now in C#?

It tends to be between 0.5 and 15 milliseconds. As a result, repeated calls to the Now property in a short time interval, such as in a loop, may return the same value.


2 Answers

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.

like image 107
Rich O'Kelly Avatar answered Sep 20 '22 06:09

Rich O'Kelly


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

like image 33
mellamokb Avatar answered Sep 18 '22 06:09

mellamokb