Jeyhun Rahimov
Jeyhun Rahimov

Reputation: 3787

How to get real date(or time)?

In mvc3 application when I create new product, I want to add it's create date.

 [HttpPost]
 public ActionResult Create( Product product )
 product.CreateDate = DateTime.Now.ToLongDateString();
 ...
  return View( product );
 }

When I will deploy site to hosting, will DateTime.Now create any problem? Does it depend on user's computers time? If user's computer time is not correct, product's create date will be incorrect..I need help about this, always to get real time. Or, advise other way, please. Sorry for my bad english..

Upvotes: 1

Views: 3338

Answers (2)

walther
walther

Reputation: 13600

DateTime.Now depends on the computer it's run on. That means, if you deploy a web application, DateTime.Now returns a DateTime set on that server. It won't return different values for different users, because C# runs on the server-side and isn't affected by a client.

Upvotes: 2

Oded
Oded

Reputation: 499352

DateTime.Now will reflect the date and time on the computer that the code is running on (so, in a web application, the server time).

I suggest using DateTime.UtcNow instead - this will give you a consistent result wherever the computer is (assuming the computer is correctly setup).

Upvotes: 5

Related Questions