Surya sasidhar
Surya sasidhar

Reputation: 30333

To get server date and time

i wrote the code to Fetch date and time from server not from client system. i write like this...

 DateTime dt = new DateTime();
 dt = DateTime.Now;

is this correct code?

Upvotes: 4

Views: 14016

Answers (4)

Ahsan
Ahsan

Reputation: 377

To get server time you need to make a class and need to modify it by server connection.

var my_date = ServerDateTime.GetServerDatetime().Date;

Get local time

var my_date = DateTime.Now;

Upvotes: 0

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

You could use:

 DateTime time = DateTime.Now;              
 string format = "MMM ddd d HH:mm yyyy";

 var dt = time.ToString(format);

Makes it aesthetically pleasing. You can format with:

 MMM     display three-letter month 
 ddd     display three-letter day of the WEEK 
 d       display day of the MONTH 
 HH      display two-digit hours on 24-hour scale 
 mm      display two-digit minutes
 yyyy    display four-digit year

Of course you can use the NTP source namespace from Google. It's some source you can download: http://www.google.com/codesearch/p?hl=en&sa=N&cd=1&ct=rc#GawBxmf1je8/NTP/NtpClient.cs&q=ntp%20lang:c%23&l=34

Upvotes: 0

qburt
qburt

Reputation: 11

yes, use the DateTime.Now to obtain the current date and time on the machine where the program resides (in the case of ASP.NET the server).

Upvotes: 1

Michael Petrotta
Michael Petrotta

Reputation: 60972

Invoking the DateTime constructor is not necessary. Use this instead:

var dt = DateTime.Now;

(This is equivalent, though slightly less verbose, than DateTime dt = DateTime.Now)

Upvotes: 3

Related Questions