3

What time does the System.DateTime.Now return in C#? Is it the Local Machine (Client) Time or Server (IIS) Time?

If this returns Client time, how to get Server time and vice-versa?

EDIT OK, sorry it was a mistake that I put paranthesis. But question stands whether local time or server time.

Firnas
  • 1,665
  • 4
  • 21
  • 31

6 Answers6

6

DateTime.Now returns current date and time on the server, expressed as the local time

To get the client time you need to use javascript:

var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
//etc
var utcHours = now.getUTCHours();
var utcMinutes = now.getUTCMinutes();
//etc
//or
var utc = now.getTime();//milliseconds since 1 January 1970 00:00:00 UTC
Jonas
  • 131
  • 3
6

DateTime.Now performs a few steps:

  • Get UTC time from the OS
  • Get timezone info from the registry of the machine on which it is executing
  • Use that timezone information to resolve UTC to local time (again, local to the executing machine)

If this returns Client time, how to get Server time and vice-versa?

If you are dealing with clients in multiple timezones, it's often useful to store all dates in UTC and then store the client's timezone for conversion purposes.

JavaScript can also be useful for resolving UTC dates to client time and/or for obtaining time in the web browser.

Geolocation (e.g. examining the user's IP) can also be used to make a guess as to the user's location/timezone, though it's not guaranteed to be accurate.

Tim M.
  • 53,671
  • 14
  • 120
  • 163
2

It returns the server time, where the website/application is hosted, as asp.net is server-side technology.

To get client time, you need to use javasrcipt/jQuery, which is client side technology:

var now = new Date();
now.format("dd/M/yy h:mm tt");

http://blog.stevenlevithan.com/archives/date-time-format http://www.w3schools.com/jsref/jsref_obj_date.asp

Hope it helps :)

Avishek
  • 1,896
  • 14
  • 33
  • Should probably be noted that `.format()` isn't a standard method; though it can be made available from a few different libraries. – Jonathan Lonowski Mar 26 '13 at 05:09
  • @JonathanLonowski : Thnx for the input. Yes, any version of the jQuery lib would do it. However, the `Date()` function is a pure javascript. – Avishek Mar 26 '13 at 05:13
1

It returns the current server time obviously when the code is on server

tariq
  • 2,193
  • 15
  • 26
1

DateTime.Now returns current date and time on server computer, expressed as the local time.

Means on the server where the code reside.

And however, use JavaScript to get the current date/time from the browser.

function getDateTime()
{
    var localTime = new Date();
    var year= localTime.getYear();
    var month= localTime.getMonth() +1;
    var date = localTime.getDate();
    var hours = localTime .getHours();
    var minutes = localTime .getMinutes();
    var seconds = localTime .getSeconds();    
    //at this point you can do with your results whatever you please
}
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

its return the current system time

Run at client : its return cllient system time if it is executing client side

Run at server: it returns server time if its executing at server

if you want server time just write a method that run at server then call it at client system

see this reference example that calls server method from client side.

Civa
  • 2,058
  • 2
  • 18
  • 30