jE ccA jE ccA - 3 months ago 19
ASP.NET (C#) Question

How to get the current time in server side?

I'm using asp.net mvc3.

Scenario:


(PC1) as server side or where I publish my project
and
(PC2) as client or where I am using my current PC
. In
PC1
the current time is
10:00 AM
and in
PC2
is
01:00 PM
with the same or different date/s. I have a button in my veiw
(cshtml)
for getting the time of the
PC1
after i click the button.

So far I tried the code
DateTime.Now()
in controller but it is getting what the current time in my current PC?.
Not sure about this one
.

How to solve this kind of problem?

Answer

Use DateTime.UtcNow() at client and server while saving the date and time. When you need to show date on your UI convert it to specific time zone using TimeZoneInfo.ConvertTimeFromUtc